0

I have the following snippet:

from decorators import before_save class User(Model): def before_save(self): pass @before_save def meth(self): pass 

It seems that when I try to decorate meth, it "uses" the instance method before_save, not the imported decorator. How can I still declare an instance method with the same name as a decorator and still be able to properly use both of them?

Shouldn't the decorator work as expected? How is it possible to refer to the before_save instance method just as before_save, without any reference to its class?

3 Answers 3

3

You can try the following

import decorators # omitted @decorators.before_save def meth(self): pass 

I wanted to use a one-liner but run into this.

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

3 Comments

I understand. But how come the code gets in some scope that allows the use of before_save to reference the newly defined instance method?
@AndreiHorak, sorry, I don't quite understand, could you rephrase your comment, please?
The defined instance method before_save shouldn't be called prepended with a reference to its class? This way the decorator would work as expected. Don't know how else I could ask better...:)
2

You can change the import name:

from decorators import before_save as before_save_decorator class User(Model): def before_save(self): pass @before_save_decorator def meth(self): pass 

That should let you use both.

1 Comment

ForceBru's solution is better if you need to change more than one import name, btw.
2

Or you could define before_save method after:

from decorators import before_save class User(Model): @before_save def meth(self): pass def before_save(self): pass 

But this is not the best solution, because it's not obvious what's going on here, and you can forget about later.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.