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


Groups > comp.lang.python > #74815

Re: Confused with Functions and decorators

Date 2014-07-19 13:10 +0100
From Wojciech Giel <wojtekgiel@gmail.com>
Subject Re: Confused with Functions and decorators
References <99846e1f-1ec1-4ed4-9ad4-5c8377b2e1f6@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.12043.1405775046.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 19/07/14 11:52, Jerry lu wrote:
> Ok so i am trying to learn this and i do not understand some of it. I also tried to searched the web but i couldnt find any answers.
>
> 1. I dont understand when i will need to use a function that returns another function.
> eg
>                     def outer():
>                           def inner():
>                                 x = 5
>                            return inner
>      
>       why not just use inner and forget about the whole outer function?
function compositing is one possible use. Functions are first class 
objects so they can be passed as arguments to other functions. in math 
you can define function composition  f(x)  * g(x) by saying that (f * g 
)(x) = f(g(x)).
so composition of two functions gives you a new function with behaviour 
the same as applying fist function to the output of the second.

ex.
you are give two functions f and g to construct their composition:

 >>> def outer(f, g):
...     def inner(x):
...         return f(g(x))
...     return inner
...
 >>> def f(x):
...     print("f ({}) called".format(x))
...     return x
...
 >>> def g(x):
...     print("g ({}) called".format(x))
...     return x
...
 >>> h = outer(f, g)
 >>> h("test")
g (test) called
f (test) called
'test'

cheers
Wojciech

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