Reputation: 3504
I'm trying to somehow 'register' a method inside a class (@classmethod
) with a decorator to be able to call it later on with ease.
So far I've tried in my decorator to get the whole 'namespace' with no results.
I'm only able to get the __module__
but I can't get the name of the class where this method resides (because I register it during the __init__
, not during __call__
inside my custom decorator).
Is there any way to achieve this?
I think that the only way is to inspect
the whole file and somehow test if the method exists inside each of the classes, so inspect
solutions are also accepted
Basically, I'm trying to fork django-dajaxice and modify this decorator to be able to register full path functions (comprensive with classname) in order to call for example my.namespace.views.MyView.as_view(...)
from AJAX (I know that it's more complicated, I'm trying to simplify)
Upvotes: 4
Views: 599
Reputation: 53819
You could use a 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 could combine this with a method decorator to annotate the methods you are interested in so you can easily identify them.
As an aside you mention @classmethod
, which is a built-in decorator that returns a function which takes a class as it's first argument. In this case you probably don't want to use (or emulate) it.
Upvotes: 1