On Sat, Nov 14, 2015 at 7:46 AM, fl <rxjw...@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. -- https://mail.python.org/mailman/listinfo/python-list