Get the namespace path function

I have a namespace, one of which is 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 one get_ns_path(func)that receives a method / function and returns a "namespace path" as a string?

A.B.C.method.im_classgives Cexcellent, but how to move on?

+3
source share
2 answers

I do not think that's possible:

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

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

+2
source

Youn can get mro with help inspect.

inspect.getmro(cls
-1
source

Source: https://habr.com/ru/post/1785773/


All Articles