Prakash Bhattarai
Prakash Bhattarai

Reputation: 79

What is the specific name of this output?

I have made a function named 'function' as below.

>>> def function():
        return 'hello world'

>>> function
<function function at 0x7fac99db3048> #this is the output

What is this output exactly? It's specific name? And it's significance? I know it gives info about memory location. But I need more information about this output.

Do the higher-order function return similar data while they are returning function?

Upvotes: 2

Views: 71

Answers (2)

Nurjan
Nurjan

Reputation: 6053

In python function is an object and thus when you call function it returns you the memory address. The higher-order functions behave the same way. However there some differences:

def a():
    print("Hello, World!")

def b():
    return a

>>> a
<function a at 0x7f8bd15ce668>
>>> b
<function b at 0x7f8bd15ce6e0>

c = b

>>>c
<function b at 0x7f8bd15ce6e0>

c = b()
<function a at 0x7f8bd15ce668>

Note what the function c returns in different situations.

Upvotes: 3

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48077

In order to call the function, you need to call it with (). Without that you are seeing reference to the function function stored at 0x7fac99db3048. You may also store it in another variable as:

>>> my_new = function  # store function object in different variable

>>> function
<function function at 0x10502bc80>
#                      ^ memory address of my system

>>> my_new
<function function at 0x10502bc80>
#                      ^ same as above

>>> my_new()    # performs same task
'hello world'

Let's see the content displayed for another function with name other than function:

>>> def hello_world():
...     print 'hello world'
...
>>> hello_world
#          v   Name of function
<function hello_world at 0x105027758>
# ^ says object of type 'function'|^- memory address of function
# (for eg: for class says 'class')|

Upvotes: 1

Related Questions