Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'parameters': 0.04; 'arguments': 0.09; 'created,': 0.09; 'none)': 0.09; 'parameter': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'creates': 0.14; 'useful,': 0.14; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'func': 0.16; 'function?': 0.16; 'integers,': 0.16; 'magic': 0.16; 'modules,': 0.16; 'parameters,': 0.16; '(you': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'things.': 0.19; 'cc:addr:python.org': 0.22; 'integer': 0.24; 'specify': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'object.': 0.31; 'though.': 0.31; 'another': 0.32; 'subject:with': 0.35; 'case,': 0.35; 'equal': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'like,': 0.36; 'being': 0.38; 'pm,': 0.38; 'anything': 0.39; 'does': 0.39; 'back': 0.62; 'times': 0.62; 'real': 0.63; 'refer': 0.63; 'more': 0.64; 'different': 0.65; 'dont': 0.67; 'yes': 0.68; 'receive': 0.70; 'jul': 0.74; 'everything.': 0.84; 'standing': 0.84; 'thing,': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=ZOH1e5Um+qwxCrqIU/saoyemxNSLCry1MzEwo8esKnU=; b=Ene5zJlILcBxGTYH2yBl5Ejrh+p8NdrvXqgEq+XEJ1OGPAZVySysu0sb8MWebNa2Ex Z99N+I5eJiFK3V1LYXF170ppkEno4fASXu7ToRda16WtakPYD4l/e0TV7HbkliM8BmlK P1S9VBsZXd0Hb7DdIxMAOTbzC/wbzqPSzZg2w7hjI45K/OJbssAvz7pCeQBF1QvsxC3D JfMWyTqmw0s2aBOMY18po23umZs8MVrN0zRh6AxQezqllfKfrAhcUPB4spzltL3/6NeC t+IP8gHmGTaPZNowYkNrRYiHqXTJOH7AtRFuLfXfzajGb+vbskuPqr1TxIjZ8TLWPUxt HfKg== MIME-Version: 1.0 X-Received: by 10.52.248.232 with SMTP id yp8mr302855vdc.83.1405767835985; Sat, 19 Jul 2014 04:03:55 -0700 (PDT) In-Reply-To: <99846e1f-1ec1-4ed4-9ad4-5c8377b2e1f6@googlegroups.com> References: <99846e1f-1ec1-4ed4-9ad4-5c8377b2e1f6@googlegroups.com> Date: Sat, 19 Jul 2014 21:03:55 +1000 Subject: Re: Confused with Functions and decorators From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1405767838 news.xs4all.nl 2970 [2001:888:2000:d::a6]:36012 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74808 On Sat, Jul 19, 2014 at 8:52 PM, 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? > > 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????? In Python, a function is a real thing, on par with integers, lists, modules, files, etc. You can create them, dispose of them, pass them as parameters, receive them as return values, store them in lists, everything. When you call outer(), it creates a function (which does nothing useful, and then returns None) and returns a reference to it. And yes, in that case, there's no particular value in that setup; but an inner function can be a "closure" (you can look that up later): def outer(x): def inner(y): return x + y return inner The x inside inner() will refer to the x that was in scope when it was created, so you can call outer() several times with different arguments and get back different functions that do different things. Since functions are objects of equal standing to any other (called "first-class" objects - look that up too), you can pass them as parameters just the same as you would any other type of object. There's no reason to call an integer 'i', so there's no reason to call a function 'func'. You can if you like, but it'll work the same regardless. There is no magic happening here. None of this has anything to do with decorators, though. ChrisA