Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Where is decorator in this example code? Date: Sat, 14 Nov 2015 09:08:33 -0700 Lines: 59 Message-ID: References: <3800f31d-e570-4492-9dcf-58105c140b2b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de yLkA5FKIrTjT7JWeqSO7/gDlHfy/DpT0piSfsn8iOQdw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'args': 0.04; 'formatting': 0.07; 'remaining': 0.07; 'subject:code': 0.07; 'args.': 0.09; 'argument,': 0.09; 'dict': 0.09; 'kwargs': 0.09; 'positional': 0.09; 'prefixed': 0.09; 'tuple': 0.09; 'python': 0.10; 'itself.': 0.11; 'def': 0.13; '**,': 0.16; 'collects': 0.16; 'declaration': 0.16; 'inverse': 0.16; 'outputs': 0.16; 'parameters,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'similarly,': 0.16; 'tuple,': 0.16; 'wrote:': 0.16; 'string': 0.17; 'passes': 0.18; 'skip:{ 20': 0.18; '2015': 0.20; 'arguments': 0.22; 'function,': 0.22; 'function:': 0.22; 'parameter': 0.22; 'pass': 0.22; 'am,': 0.23; "python's": 0.23; 'sat,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; 'sequence': 0.27; 'function': 0.28; '**kwargs)': 0.29; '**kwargs):': 0.29; 'rules': 0.31; 'language.': 0.32; 'changed': 0.33; 'class': 0.33; 'problem': 0.33; 'list': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'quite': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'assigned': 0.36; 'keyword': 0.36; 'skip:{ 10': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'version': 0.38; 'received:209': 0.38; 'along': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'your': 0.60; 'more': 0.63; 'different': 0.63; 'roles': 0.66; 'manner': 0.69; 'transferred': 0.72; 'dict,': 0.84; 'dict.': 0.84; 'f(*args,': 0.84; 'subject:Where': 0.84; 'to:name:python': 0.84; 'subject:this': 0.85 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=izEXI44qz9YnfwLJc0KBkzYrMmFYEHXKfmar/1CUdTs=; b=Dd2vgQ/QTlKnGndo8EfUokWuWUIoW6XSqQ20dQDyCc9rRBKfQ2hgw2N0rWaH2ynH/o qEsnsTFRWxOn6L+4lnnmHnIwd1YXnH9MSfDnyvrDe1pQkK+NE2dGyJAZGe997HdN2etz f3esWqqhUb18EtsEAJe012SBJC61lpZhdJOO/hlbfsvwWBpzsio8UJTtPqIodAbDvElG dTeH4c4Lta9fAaml50HfzNkChGCOXRp8r0SQ0EGnYlWIZlNO+rOFMAWHS1R1oH4tBovP rp+pusbj28bTSbXRnyeGR2Wq1HFH27jBt988UWF1/ToAcblFKWRdL9cntCd8kOZJUzCw 4P7A== X-Received: by 10.107.19.12 with SMTP id b12mr29321010ioj.11.1447517352784; Sat, 14 Nov 2015 08:09:12 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98814 On Sat, Nov 14, 2015 at 7:46 AM, fl 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 "

{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) 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.