Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'output': 0.05; 'arguments': 0.09; 'second.': 0.09; 'def': 0.12; 'cheers': 0.12; 'function?': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'math': 0.24; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; '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; '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; 'dont': 0.67; '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=9J3aYdXUHX8NYTVIkFkKmZh9aF5mydcO6BPoZWBODHU=; b=rqr/LN0KDR/zKxX4CVCKDm8tGtV0KqodeUv9PT3/TxtnFI9rW/Hj9WI/ivYjqUGwbL gZxlZkwti5Mk7GaQhgtJl1cHV32CW3CiTM/JFT8q0s2lFmZTuoe9Y0lxXSjI9InGJi6k keoRPlFqM27pxlZWOxVbavYKvfFPLoIZ3kROiVrGIQZASAD3MRzdD6eHk9Ogdeu1FPFl rNmwIkP9feka7lB0OOat/jq2TlpLS+mmCMBXhkhw3CDBwmtR13OlnDQLZfM9Qk269dSf 3cyzZAZH652ivcL3O9nHYRv8UWgYGjEk6f5WJKJ753BNRvbb2xopi/X6HSe/83YPeI2X RpcQ== X-Received: by 10.180.20.14 with SMTP id j14mr15326764wie.48.1405771816483; Sat, 19 Jul 2014 05:10:16 -0700 (PDT) Date: Sat, 19 Jul 2014 13:10:14 +0100 From: Wojciech Giel 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1405775046 news.xs4all.nl 2906 [2001:888:2000:d::a6]:55872 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74815 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