Get classname method inside decorator on __init__ in python

I am trying somehow to "register" a method inside a class ( @classmethod ) using a decorator to be able to call it later.

So far, I have been trying in my decorator to get the full namespace without any results.

I can only get __module__ , but I can’t get the name of the class this method is in (because I register it during __init__ , and not during __call__ inside my custom decorator).

Is there any way to achieve this?

I think the only way to inspect whole file is to somehow check if the method exists inside each of the classes, so inspect decisions are also made

Additional Information

Basically, I'm trying to develop django-dajaxice and change this decorator to be able to register full path functions (consisting of classname) to call, for example, my.namespace.views.MyView.as_view(...) from AJAX (I know that it is more difficult, I try to simplify)

+4
source share
1 answer

You can use the class decorator to register your methods instead:

 def register_methods(cls): for name, method in cls.__dict__.items(): # register the methods you are interested in @register_methods class Foo(object): def x(self): pass 

You can combine this with a method decorator to annotate the methods that interest you so that you can easily identify them.

Alternatively, you will mention @classmethod , which is a built-in decorator that returns a function that takes a class as its first argument. In this case, you probably don't want to use (or emulate) it.

+1
source

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


All Articles