X-Received: by 10.129.83.85 with SMTP id h82mr23356086ywb.0.1447512377576; Sat, 14 Nov 2015 06:46:17 -0800 (PST) X-Received: by 10.50.142.39 with SMTP id rt7mr172122igb.7.1447512377529; Sat, 14 Nov 2015 06:46:17 -0800 (PST) Path: csiph.com!au2pb.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!i2no3083851igv.0!news-out.google.com!l1ni4218igd.0!nntp.google.com!i2no3083835igv.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Sat, 14 Nov 2015 06:46:16 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.100.117.144; posting-account=SZ_svQkAAACWRFG2bDA-zgq8ILyl4-vo NNTP-Posting-Host: 50.100.117.144 References: <3800f31d-e570-4492-9dcf-58105c140b2b@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Where is decorator in this example code? From: fl Injection-Date: Sat, 14 Nov 2015 14:46:17 +0000 Content-Type: text/plain; charset=ISO-8859-1 Lines: 89 Xref: csiph.com comp.lang.python:98808 On Saturday, November 14, 2015 at 7:38:09 AM UTC-5, Chris Warrick wrote: > On 14 November 2015 at 13:13, fl wrote: > > 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? > [snip code] > > > > 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, > > -- > > https://mail.python.org/mailman/listinfo/python-list > > Have you tried executing the code with and without the decorator? > > $ python2 without-decorator.py > John Doe > $ python2 with-decorator.py >

John Doe

> > Basically, the decorator wraps the output of your get_fullname > function with HTML

tags. > > (Running code examples is a great way to understand them.) > > -- > Chris Warrick > PGP: 5EAAEA16 Thanks. I did run the code, but I did not check the difference carefully between them. A following problem now is about the args in class decorate. I do not see args and kwargs are transferred by get_fullname(self). If I change return "

{0}

".format(func(*args, **kwargs)) to return "

{0}

".format(func(*args)) The outputs are the same. But it is quite different if it is changed to: return "

{0}

".format(func) What roles are args and kwargs? I know C language. For Python here, I don't see some rules on the args. Thanks again. ///////////// def p_decorate(func): def func_wrapper(*args, **kwargs): return "

{0}

".format(func(*args, **kwargs)) 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()