Where is decorator in this example code?
fl
rxjwg98 at gmail.com
Sat Nov 14 07:13:19 EST 2015
On Saturday, November 14, 2015 at 7:11:11 AM UTC-5, fl wrote:
> Hi,
>
> I am learning decorator following this link:
>
> http://thecodeship.com/patterns/guide-to-python-function-decorators/
>
> When I read decorator on class, I don't see decorator taking in effect.
> In the following code snippet, there is the same print out if I comment out
> two lines 'def p_decorate(func):' and '@p_decorate'.
>
> Can you tell me what role of decorator in this code?
>
>
> Thanks,
>
> ......
> def p_decorate(func):
> def func_wrapper(self):
> return "<p>{0}</p>".format(func(self))
> return func_wrapper
>
> class Person(object):
> def __init__(self):
> self.name = "John"
> self.family = "Doe"
>
> @p_decorate
> def get_fullname(self):
> return self.name+" "+self.family
>
> my_person = Person()
> print my_person.get_fullname()
My OP may not be clear enough. Here is the key question. After the function
definition, there is no obvious decorator application in the function call:
my_person = Person()
print my_person.get_fullname()
Where is the difference between the non-decorator and decorator in this
example?
Thanks,
More information about the Python-list
mailing list