[Tutor] Passing functions as arguments to other functions
boB Stepp
robertvstepp at gmail.com
Fri Sep 30 20:36:57 EDT 2016
On Fri, Sep 30, 2016 at 3:43 AM, Alan Gauld via Tutor <tutor at python.org> wrote:
> On 30/09/16 03:43, boB Stepp wrote:
>
>> Also, I note that if I just type a function name without the
>> parentheses in the interpreter, I will get something like this:
>>
>>>>> def f():
>> pass
>>
>>>>> f
>> <function f at 0x000001F775A97B70>
>>
>> So the impression I am getting is that a function name by itself (with
>> no parentheses) is the function *object*.
>
> No.
> The function name is just a name like any other name in Python
I understand this. It gets tiring to be precise and say something
like, "So the impression I am getting is that a function name by
itself (with no parentheses) references the function object." But I
probably should strive to be more precise in my writing as you
otherwise have to guess what I know and don't know.
>> But why does Python require separating the function object
>> from its parameters when it is being passed as an argument
>> to another function?
>
> g = f
>
> causes g to become a second reference to the function object
>
> g = f(x)
>
> causes g to become a reference to the result of calling
> the function object referred to by f, passing in the object
> referred to by x.
>
> The parentheses in this case are not there as a container
> of the object x they are there as an indication that f
> is being called.
I think this was my key point of confusion. I was mistakenly thinking
of f(x) as referring to the function object. Instead, it is calling
that object with argument x, which is two separate things.
> HTH
It does. Thanks, Alan!
--
boB
More information about the Tutor
mailing list