How to access static members of outer class from an inner class constructor?
How to get this to work:
class ABC(object):
C1 = 1
class DEF(object):
def __init__(self, v=ABC.C1):
self.v = v
a = ABC()
From the inne开发者_Go百科r class DEF, I cannot access the constant "C1". I tried with "ABC.C1" and "C1" alone but to no avail.
Please advise.
I advice you to not nest classes. Why do you do that? There is no reason to do that, ever. This works:
>>> class ABC(object):
... C1 = 1
...
>>> class DEF(object):
... def __init__(self, v=ABC.C1):
... self.v = v
...
>>> a = ABC()
>>> d = DEF()
>>> d.v
1
The reason your code doesn't work is that ABC doesn't exist yet, as it's not been constructed. You can't access the ABC class from within it's own construction, it's not created until the end.
精彩评论