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


Groups > comp.lang.python > #98814

Re: Where is decorator in this example code?

From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Where is decorator in this example code?
Date 2015-11-14 09:08 -0700
Message-ID <mailman.330.1447517363.16136.python-list@python.org> (permalink)
References <3800f31d-e570-4492-9dcf-58105c140b2b@googlegroups.com> <bf73e0ab-2394-42a6-bb53-dd73be3897e3@googlegroups.com> <mailman.324.1447504672.16136.python-list@python.org> <e1a11976-9fb5-4df1-a84e-73fd76a17ebe@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web