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


Groups > comp.lang.python > #74909

Re: Confused with Functions and decorators

Newsgroups comp.lang.python
Date 2014-07-21 00:30 -0700
References <99846e1f-1ec1-4ed4-9ad4-5c8377b2e1f6@googlegroups.com> <d45c2447-4dc8-47f1-b6fd-df5e1618fc77@googlegroups.com> <mailman.12044.1405775046.18130.python-list@python.org>
Message-ID <49a2e178-62e7-4186-a32c-488a2fa530a6@googlegroups.com> (permalink)
Subject Re: Confused with Functions and decorators
From CHIN Dihedral <dihedral88888@gmail.com>

Show all headers | View raw


On Saturday, July 19, 2014 8:44:25 PM UTC+8, Wojciech Giel wrote:
> 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.

Uhn, a local object inside a function
can be passed back in Python.

Of course, a local function is treated
as an object in Python,and the GC is
built-in. 


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