Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get function's 'namespace path'

Tags:

python

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.C.method.im_class gives C, great, but how to go further up?

like image 236
kolypto Avatar asked Mar 11 '26 22:03

kolypto


1 Answers

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?

like image 122
Jasmijn Avatar answered Mar 14 '26 10:03

Jasmijn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!