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


Groups > comp.lang.python > #74814

Re: Confused with Functions and decorators

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fdn.fr!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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.027
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'output': 0.05; 'arguments': 0.09; 'parameter': 0.09; 'second.': 0.09; 'def': 0.12; 'func': 0.16; 'function?': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'specify': 0.24; 'math': 0.24; 'question': 0.24; 'define': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'statement': 0.30; 'gives': 0.31; 'class': 0.32; 'another': 0.32; 'subject:with': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'functions.': 0.36; 'possible': 0.36; 'two': 0.37; 'being': 0.38; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'use.': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'new': 0.61; 'first': 0.61; 'more': 0.64; 'dont': 0.67; 'yes': 0.68; 'applying': 0.72; "'test'": 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=C4BGT6eJrsKW7HXtZ9ogkr0AGNkczYT0c7VuK86Zv8E=; b=nLmL12GMmTrhkzcGdenGaKbZKbE2szhapnr61a8vrAhYRiTmApHF41N0DaPFTfIRaU 2IVfgFR5YQpcZtuNZDr+YHFP6Wf0f8ZjtkZFAPTLTZD9fFIRv+Q4iAFDOfiLhyAZuT5x Fzrs29IPq5+u1FTSfasuo9SeI73EPgvRYV+lkEy4Gl2guNiXNTdw6TJ/xi/hmSko9XNB /hjIQkvJv3LIa0AAwAsYQ7jQV8F+S1OG01DkZQ9PyMcyuhgUEPno6ZtIzYV5JCWdaCqo HMLKlht2dOqSMkR3K2S+weZUU6SAdGH3qIO+OYyJllI0rrw1eTykvMTrudy3f4fgYkz3 uk+w==
X-Received by 10.180.74.198 with SMTP id w6mr3762224wiv.7.1405771580863; Sat, 19 Jul 2014 05:06:20 -0700 (PDT)
Date Sat, 19 Jul 2014 13:06:19 +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>
In-Reply-To <99846e1f-1ec1-4ed4-9ad4-5c8377b2e1f6@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.12042.1405775046.18130.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405775046 news.xs4all.nl 2905 [2001:888:2000:d::a6]:55871
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74814

Show key headers only | 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'
> 2. This is more yes or no question when you pass in a func into another func as a parameter do you need to specify the parameter that the func is being passed into as func?
> eg
>                        def passinto(func)
>                              pass
>              
>                         def param():
>                               x = 5
>                     
>                          p = passinto(param)
>
> also is this above statement correct?????
>       

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