开发者

Get function's 'namespace path'

I have some namespaces, one included in the other:

class A:
    class B:
        class C:
            def method(): pass

get_ns_path(A.B.C.method) # >>> 'A.B.C.method'

Is it possible to implement such get_ns_path(func) that receives a method/function and returns the 'namespace path' as a string?

A.B开发者_JS百科.C.method.im_class gives C, great, but how to go further up?


I don't think that is possible:

>>> dir(A.B.C)
['__doc__', '__module__', 'method']

More convincingly, there's no reason A.B.C should know about A.B, because you can do Z.C = A.B.C and they would be the same object. So what would get_ns_path(Z.C.method) return?


Youn can get the mro using the inspect module.

inspect.getmro(cls)¶
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜