I'd like to initialize the class data all at once instead of after the class is declared. I realize the class variables / methods aren't accessible during the definition.
I've done the following to be able to initialize a variable based on class variable. I'm not thrilled with this, but I haven't seen something I like.
so:
- what are the downsides to doing what I've done?
- would it better to just initialize the rest of the class after the definition? that just looks wrong to me, but I'm new to python and still getting used to it.
class foo:
stuff=[1,3,5,7]
@property
def data(self):
print('...init...')
value = sum(self.stuff)
setattr(foo, 'data', value)
return value
def __init__(self, x):
self.x = x
f=foo(1)
g=foo(2)
print(f.data)
print(f.data)
print(g.data)
print()
print(f.x)
print(g.x)
Output
...init...
16
16
16
1
2