Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98803 > unrolled thread

Where is decorator in this example code?

Started byfl <rxjwg98@gmail.com>
First post2015-11-14 04:10 -0800
Last post2015-11-14 09:08 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Where is decorator in this example code? fl <rxjwg98@gmail.com> - 2015-11-14 04:10 -0800
    Re: Where is decorator in this example code? fl <rxjwg98@gmail.com> - 2015-11-14 04:13 -0800
      Re: Where is decorator in this example code? Chris Warrick <kwpolska@gmail.com> - 2015-11-14 13:37 +0100
        Re: Where is decorator in this example code? fl <rxjwg98@gmail.com> - 2015-11-14 06:46 -0800
          Re: Where is decorator in this example code? Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-14 09:08 -0700

#98803 — Where is decorator in this example code?

Fromfl <rxjwg98@gmail.com>
Date2015-11-14 04:10 -0800
SubjectWhere is decorator in this example code?
Message-ID<3800f31d-e570-4492-9dcf-58105c140b2b@googlegroups.com>
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()

[toc] | [next] | [standalone]


#98804

Fromfl <rxjwg98@gmail.com>
Date2015-11-14 04:13 -0800
Message-ID<bf73e0ab-2394-42a6-bb53-dd73be3897e3@googlegroups.com>
In reply to#98803
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,

[toc] | [prev] | [next] | [standalone]


#98805

FromChris Warrick <kwpolska@gmail.com>
Date2015-11-14 13:37 +0100
Message-ID<mailman.324.1447504672.16136.python-list@python.org>
In reply to#98804
On 14 November 2015 at 13:13, fl <rxjwg98@gmail.com> 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
<p>John Doe</p>

Basically, the decorator wraps the output of your get_fullname
function with HTML <p> tags.

(Running code examples is a great way to understand them.)

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

[toc] | [prev] | [next] | [standalone]


#98808

Fromfl <rxjwg98@gmail.com>
Date2015-11-14 06:46 -0800
Message-ID<e1a11976-9fb5-4df1-a84e-73fd76a17ebe@googlegroups.com>
In reply to#98805
On Saturday, November 14, 2015 at 7:38:09 AM UTC-5, Chris Warrick wrote:
> On 14 November 2015 at 13:13, fl <rx**g98@gmail.com> 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
> <p>John Doe</p>
> 
> Basically, the decorator wraps the output of your get_fullname
> function with HTML <p> tags.
> 
> (Running code examples is a great way to understand them.)
> 
> -- 
> Chris Warrick <https://chriswarrick.com/>
> 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 "<p>{0}</p>".format(func(*args, **kwargs))

to

return "<p>{0}</p>".format(func(*args))

The outputs are the same.

But it is quite different if it is changed to:

return "<p>{0}</p>".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 "<p>{0}</p>".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()

[toc] | [prev] | [next] | [standalone]


#98814

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-11-14 09:08 -0700
Message-ID<mailman.330.1447517363.16136.python-list@python.org>
In reply to#98808
On Sat, Nov 14, 2015 at 7:46 AM, fl <rxjwg98@gmail.com> wrote:
> 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 "<p>{0}</p>".format(func(*args, **kwargs))
>
> to
>
> return "<p>{0}</p>".format(func(*args))
>
> The outputs are the same.
>
> But it is quite different if it is changed to:
>
> return "<p>{0}</p>".format(func)

In this case you're not even calling the function, so the thing that
you're formatting in the string is the function object itself.

> What roles are args and kwargs? I know C language.
> For Python here, I don't see some rules on the args.

They are Python's version of "varargs" or variadic arguments.

If a function parameter declaration is prefixed with *, then that
parameter will collect all the remaining positional arguments. For
example, with the function declaration "def f(a, b, *c):", f may be
called with two or more arguments. The first two arguments will be
assigned to a and b, and c will be a tuple containing all the
remaining arguments. It is customary but not necessary to name this
parameter "args".

You can also do the inverse when calling a function. If x is a list or
tuple, then calling f(x) will pass the sequence x to f as a single
argument, while calling f(*x) will pass the contents of x as
individual arguments.

Similarly, if a function parameter declaration is prefixed with **,
then that parameter will collect all the keyword arguments that have
not been assigned to other parameters, in a dict. It is customary but
not necessary to name this parameter "kwargs". And again you can also
do the inverse when calling a function: if x is a dict, then calling
f(x) will pass along the dict as a single argument, while calling
f(**x) will pass the contents of the dict as individual keyword
arguments.

So if you have a function like this:

    def f(*args, **kwargs):
        return g(*args, **kwargs)

This collects all the arguments that were passed to f, and passes them
along to g in the same manner they were supplied to f.

In your example, removing **kwargs appeared to do nothing because no
keyword arguments were actually passed in.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web