Reputation: 31
i need help with printing attributes from nested class.
class SimpleClass(object):
class NestedClass(object):
def __init__(self):
self.attribute = 1
...
print(self.SimpleClass.attribute) # this doesnt work, how can i print attribute ?
I really dont know how to call it, i tried everything i knew, e.g.:
print(SimpleClass.NestedClass.attribute)
print(NestedClass.attribute)
... but nothing works
Thanks.
Upvotes: 0
Views: 89
Reputation: 23624
attribute
is only created for an instance of NestedClass
after the constructor is called. There is no initial value of attribute
. If you did want to have a default value then you could do something like..
class SimpleClass(object):
class NestedClass(object):
attribute = 0
def __init__(self):
self.attribute = 1
The base class starting value of attribute
is 0, and any instances will be 1
>>> SimpleClass.NestedClass.attribute
0
>>> SimpleClass.NestedClass().attribute
1
>>>
Upvotes: 2
Reputation: 4318
You are missing braces:
print(SimpleClass().NestedClass().attribute)
or
print(SimpleClass.NestedClass().attribute)
Upvotes: 0