We can easily determine an object's mro, by accessing its __mro__ attribute.
In my case I have a really complex hierarchy that I'm trying to untangle to avoid the current mro hell we are undergoing.
I have a very long mro chain like this one:
(<class 'CompanyUserViewSet'>, <class 'CompanyGenericViewSet'>,
<class 'CompanyDispatchMixin'>, <class 'CompanyCorsLiteMixin'>,
<class 'CorsLiteMixin'>, <class 'ErrorHandlingMixin'>, ..., <type 'object'>)
This is shortened for the sake of the question, but in this case it sums up to 19 classes.
I currently have a problem: I need to know from which one of these classes python is resolving the as_view method.
I know I can check these in order, but I fail to see what I'm doing wrong, since the as_view that is getting called is not the correct one.
The call to the method is pretty simple, we have a CompanyUserViewSet that Rest Framework is using to build the urls for a router:
view = viewset.as_view(mapping, **route.initkwargs)
How can I determine, given an object and one of its attributes (or methods) from what class is python resolving it?
To get where a method is defined use, method.__func__.__qualname__. That requires Python >= 3.3. In older pythons you can use the qualname package.
See the example below:
class A:
def f1():
return 'f1A'
def f2():
return 'f2A'
class B(A):
def f2():
return 'f2B'
a = A()
b = B()
print(a.f1.__func__.__qualname__)
print(a.f2.__func__.__qualname__)
print(b.f1.__func__.__qualname__)
print(b.f2.__func__.__qualname__)
which produces the output:
A.f1
A.f2
A.f1
B.f2
If you are using the qualname package:
import qualname
print qualname.qualname(a.f1)
print qualname.qualname(a.f2)
print qualname.qualname(b.f1)
print qualname.qualname(b.f2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With