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


Groups > comp.lang.python > #74816

Re: Confused with Functions and decorators

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <wojtekgiel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'syntax': 0.04; 'definitions': 0.07; 'def': 0.12; 'cheers': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'func': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'passing': 0.19; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'decorators': 0.24; 'define': 0.26; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'forgot': 0.30; 'yes.': 0.31; 'another': 0.32; 'subject:with': 0.35; 'created': 0.35; 'definition': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'track': 0.38; 'to:addr:python.org': 0.39; 'first': 0.61; 'name': 0.63; 'refer': 0.63; 'decorate': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=ndBHHpScish7PeHZU6NlRg/uuTqCoTBlmEdh8IKBsQ8=; b=qtzfVgyrF9B0z9tICoetz50tb9JjffgCeiilDJRzdpLloFQshI/XTu3hagtlsa9fgK wWkaye1POo68fMgoXdAptiBKQvHDnVmbY7jhPaPZXkg0S5CaylSplD4M59XkIu32wKHJ cNxexzh5tWGfTzIOPbJxtCtQpCUk++vqFgY5jJgQ6RVKDa3et+6T97+0eGwpfAaxdNHi Hk07l1HddfLV32IeNmgAvmMWqNQmmyhpTZpdNtZoth4Ms4unEawqt7h85Av64gg97/5/ XDo6cmKZsV8q62jToFiR/6gGlcZfQnn7K+h2HobI6/xergH8jLU0IhYmGJKkNvqB99gh iTNA==
X-Received by 10.180.13.139 with SMTP id h11mr41517937wic.40.1405773866861; Sat, 19 Jul 2014 05:44:26 -0700 (PDT)
Date Sat, 19 Jul 2014 13:44:25 +0100
From Wojciech Giel <wojtekgiel@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Confused with Functions and decorators
References <99846e1f-1ec1-4ed4-9ad4-5c8377b2e1f6@googlegroups.com> <d45c2447-4dc8-47f1-b6fd-df5e1618fc77@googlegroups.com>
In-Reply-To <d45c2447-4dc8-47f1-b6fd-df5e1618fc77@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Mailman-Approved-At Sat, 19 Jul 2014 15:04:05 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12044.1405775046.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405775046 news.xs4all.nl 2908 [2001:888:2000:d::a6]:55873
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74816

Show key headers only | View raw


On 19/07/14 12:40, Jerry lu wrote:
> oh yeah i forgot about the decorators. Um say that you wanted to decorate a function with the outer() func you would just put @outer on top of it? And this is the same as passing another func into the outer func?
yes.
syntax was added because with very long function definitions it was 
dificult  to track reassignment to the name when it followed definition 
of the function. decorators is just abbreviation.

 >>> def outer(f):
...     def inner(*args, **kwargs):
...         print("inner function")
...         return f(*args, **kwargs)
...     return inner
...
 >>> @outer
... def myfunc(x):
...     print("Myfunc", x)
...
 >>> myfunc("test")
inner function
Myfunc test

it is exactly equivalent to:

 >>> def outer(f):
...     def inner(*args, **kwargs):
...         print("inner function")
...         return f(*args, **kwargs)
...     return inner
...
 >>> def myfunc(x):
...      print("Myfunc", x)
...
 >>> myfunc = outer(myfunc)
 >>> myfunc("test")
inner function
Myfunc test

cheers
Wojciech
>
> and also with the first example you say x is in the scope when is was created can you define x in the outer func and refer to it in the inner func?
check nonlocal.

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


Thread

Confused with Functions and decorators Jerry lu <nicholascannon1@gmail.com> - 2014-07-19 03:52 -0700
  Re: Confused with Functions and decorators Chris Angelico <rosuav@gmail.com> - 2014-07-19 21:03 +1000
  Re: Confused with Functions and decorators Jerry lu <nicholascannon1@gmail.com> - 2014-07-19 04:40 -0700
    Re: Confused with Functions and decorators Chris Angelico <rosuav@gmail.com> - 2014-07-19 21:50 +1000
    Re: Confused with Functions and decorators Wojciech Giel <wojtekgiel@gmail.com> - 2014-07-19 13:44 +0100
      Re: Confused with Functions and decorators CHIN Dihedral <dihedral88888@gmail.com> - 2014-07-21 00:30 -0700
        Re: Confused with Functions and decorators Steven D'Aprano <steve@pearwood.info> - 2014-07-21 07:45 +0000
  Re: Confused with Functions and decorators Jerry lu <nicholascannon1@gmail.com> - 2014-07-19 05:01 -0700
  Re: Confused with Functions and decorators Wojciech Giel <wojtekgiel@gmail.com> - 2014-07-19 13:06 +0100
  Re: Confused with Functions and decorators Wojciech Giel <wojtekgiel@gmail.com> - 2014-07-19 13:10 +0100
  Re: Confused with Functions and decorators Jerry lu <nicholascannon1@gmail.com> - 2014-07-19 19:24 -0700
    Re: Confused with Functions and decorators Chris Angelico <rosuav@gmail.com> - 2014-07-20 12:27 +1000
      Re: Confused with Functions and decorators Jerry lu <nicholascannon1@gmail.com> - 2014-07-19 19:33 -0700
  Re: Confused with Functions and decorators Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-20 06:41 +0000

csiph-web