开发者

Get function's 'namespace path'

开发者 https://www.devze.com 2023-02-05 04:01 出处:网络
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\'

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

精彩评论

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

关注公众号