Skip to content Skip to sidebar Skip to footer

Python: How To Get Outer Class Variables From Inner Static Class?

I want to specify variable once by making instance Outer(variable), than this variable use in all static classes, how should I do that? Is there any other solution than use not sta

Solution 1:

If you really want such thing, metaclass may help a little, for example:

from types import ClassType

classOuterMeta(type):
    def__new__(mcls, name, base, attr):
        ret = type.__new__(mcls, name, base, attr)
        for k, v in attr.iteritems():
            ifisinstance(v, (ClassType, type)):
                v.Outer = ret
        return ret

classOuter(object):
    __metaclass__ = OuterMeta
    var = 'abc'classInner:
        defwork(self):
            print self.Outer.var
        @classmethoddefwork2(cls):
            print cls.Outer.var

then

>>>Outer.Inner.work2()
abc

Solution 2:

No. Inner-class methods have no way of accessing instances of the outer class.

Post a Comment for "Python: How To Get Outer Class Variables From Inner Static Class?"