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
The statement data_member = generate_data()
will be executed only once, when class coo: ...
is executed. In majority of cases class statements occur at module level and are executed when module is imported. So data_member = generate_data()
will be executed only once when you import module with class coo
for the first time.
All instances of coo
class will share data_member
and can access it by writing coo.data_member
. Any changes made to coo.data_member
will be immediately visible by any coo
instance. An instance can have its own data_member
attribute. This attribute can be set by typing self.data_member = ...
and will be visible only to that instance. The "static" data_member
can still be accessed by typing coo.data_member
.