0

I'm using Google's Int32Value protobuf in python. I'm wondering what the proper way to set the value is. If we do it during initialization, then we can do

my_int_32_value = Int32Value(value=123)

If we try to set it after initialization, then is this the proper way?

from google.protobuf.wrappers_pb2 import Int32Value

my_int_32_value = Int32Value()
my_int_32_value.value = 123

It seems to work, but also feels wrong to directly access the attribute like that, not via a setter.

EDIT: For instance, to set a Timestamp after it has been created, you can invoke the FromDateTime function, or some version of MergeFrom or CopyFrom

information_interchange
  • 2,538
  • 6
  • 31
  • 49
  • What does the documentation say about it? Why do you suppose that this is not "a setter"? What do you expect "setters" to look like in Python? Why? – Karl Knechtel Apr 21 '22 at 18:01
  • the docs don't say anything about using the actual class. I suppose I forgot to add that in the past, I have used `MergeFrom` and `CopyFrom` syntax; certain WellKnownTypes have specific constructor functions; e.g. https://googleapis.dev/python/protobuf/latest/google/protobuf/timestamp_pb2.html#google.protobuf.timestamp_pb2.Timestamp.FromDatetime – information_interchange Apr 21 '22 at 18:48
  • 1
    That seems like a perfectly ordinary way to change the value of an object that has a value. If they wanted to protect, they'd use property setters. – Tim Roberts Apr 21 '22 at 18:51
  • Alright, thank you. It may have been a question arising from python inexperience; I suppose the pythonic way is to not use a setter https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters – information_interchange Apr 22 '22 at 04:53

0 Answers0