4

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

More Infos

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)

1
  • 4
    Your question is rather unclear to me. Could you show some code? A broader description of the problem you are trying to solve would also be appreciated -- probably there is an easier way to do it. Commented Feb 15, 2012 at 13:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I like this approach, but still, this will reduce the flexibility of the code, because I would have to manually add the methods each time I create something new. I think that's the only way without inspecting the module isn't it ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.