I have a class with a complex data member that I want to keep \"static\". I want to initialize it once, using a function. How Pythonic is something like this:
de
As others have answered you're right -- I'll add one more thing to be aware of: If an instance modifies the object coo.data_member
itself, for example
self.data_member.append('foo')
then the modification is seen by the rest of the instances. However if you do
self.data_member = new_object
then a new instance member is created which overrides the class member and is only visible to that instance, not the others. The difference is not always easy to spot, for example self.data_member += 'foo'
vs. self.data_member = self.data_member + 'foo'
.
To avoid this you probably should always refer to the object as coo.data_member
(not through self
).