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


Groups > comp.lang.python > #74808

Re: Confused with Functions and decorators

References <99846e1f-1ec1-4ed4-9ad4-5c8377b2e1f6@googlegroups.com>
Date 2014-07-19 21:03 +1000
Subject Re: Confused with Functions and decorators
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.12039.1405767838.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Jul 19, 2014 at 8:52 PM, Jerry lu <nicholascannon1@gmail.com> 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

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