开发者

How to find out the nested class hierarchy?

开发者 https://www.devze.com 2022-12-12 11:04 出处:网络
I have a python code like this. File named mymodule.py class MyBase(object): pass File named data.py from mymodule import MyBase

I have a python code like this.

File named mymodule.py

class MyBase(object):
    pass

File named data.py

from mymodule import MyBase

class A:
    class NestA(MyBase):
        pass
    class NestB(MyBase):
        pass

class B:
    class NestA(MyBase):
        pass
    class NestB(MyBase):
        pass

if I h开发者_StackOverflowave a = A.NestA (not it is referring to a class, a is not the object of class NestA but the class itself) how do I find out what nested class hierarchy does a belong to? a.name gives me NestA so that is not a problem. I want to find out what outer class NestA is part of, i.e class A or class B. How do I do it?


You can do this with the inspect module:

import inspect

a = A.NestA

print a in [x[1] for x in inspect.getmembers(A, inspect.isclass)]
print a in [x[1] for x in inspect.getmembers(B, inspect.isclass)]

Result:

True
False

Addendum:

If you don't know anything about the classes in the module, you can backtrack and get the module.

# for each class in a's module...
for klass in inspect.getmembers(inspect.getmodule(a), inspect.isclass):
    # see if a is in that class
    if a in [x[1] for x in inspect.getmembers(klass[1], inspect.isclass)]:
        print a, "is a member of", klass[0]

Result:

__main__.NestA is a member of A


You can use __qualname__ to get the nested class hierarchy,

A.NestA.__qualname__ == 'A.NestA'


You can do something like this with metaclass programming.

class SetOuterClassType(type):
    def __init__(cls, name, bases, attrs):
        for attrname, attrvalue in attrs.iteritems():
            if getattr(attrvalue, '__set_outerclass__', False):
                attrvalue.__outerclass__ = cls

class OuterClassSetter(object):
    __metaclass__ = SetOuterClassType

class MyBase(object):
    @classmethod
    def fullname(cls):
        if hasattr(cls,'__outerclass__'):
            return '%s.%s' % (
                cls.__outerclass__.__name__, cls.__name__ )
        else:
            return '%s' % cls.__name__


class A(OuterClassSetter):
    class NestA(MyBase):
        __set_outerclass__ = True
    class NestB(MyBase):
        __set_outerclass__ = True


class B(OuterClassSetter):
    class NestA(MyBase):
        __set_outerclass__ = True

    class NestB(MyBase):
        __set_outerclass__ = True

print A.NestA.fullname() # prints 'A.NestA'
print A.NestB.fullname() # prints 'A.NestB'
print B.NestA.fullname() # prints 'B.NestA'
print B.NestB.fullname() # prints 'B.NestB'
0

精彩评论

暂无评论...
验证码 换一张
取 消