Passing Python function with no argument

Question:

I’ve tried Googling this, but not finding a definitive answer:

why, when you run the following code:

n = "Hello"
# Your function here!
def string_function(n):
    return (n) + 'world'
print string_function

You get the following response:

<function string_function at 0x7f5111010938>
None 

I realize of course that this function has no argument, but what specifically is this response? What does that string of letters and numbers mean (“0x7f5111010938”)?

Thanks for any info!

Answers:

Whatever you have got is a function object. It can be used anywhere further in your program. The list is the address of the function in memory. Thus

f = string_function #is a function object

f now can be used as a function variable and can be used as f('hi')

Answered By: Bhargav Rao

Python saving all objects with unique id. It’s a memory adress of that function and every object is actually a class you can check it with;

print (type(string_function))

>>> 
<class 'function'>
>>>

or;

>>> a="hello"
>>> print (type(a))
<class 'str'>
>>> x=26
>>> print (type(x))
<class 'int'>
>>>  

As you see class, and obviously if you call your function print(string_function()) with this you will see an error cuz of missing arguments in function

Answered By: GLHF

The output is the function object itself. The word function is the type of the function object you printed. The part string_function is the name of the object. The numbers are the address of the function object in memory.

Everything in Python is a first class object, including functions and classes. Functions even have their own function type in Python (which you can discover by just doing type(myfunction))- just like 100 is an int, 'Hello World' is a str, True is a bool, or myfancyobject is a MyFancyClass instance.

You can, for example, pass functions (or even classes themselves, which are of the type type) into other functions:

def increment(x): return x + 1
def sayHiBefore(func, *args, **kwargs):
    print('Hi')
    return func(*args, **kwargs)

print(sayHiBefore(increment, 1))

This is obviously a pretty contrived illustration, but being able to pass around functions this way turns out to be extremely useful. Decorators are a good example of something useful you can do with this ability. Google “python decorators” to find out more about them.

Another thing you can do with objects is give them attributes! You can even do things like this:

increment.a = 1

Pretty much anything you can do to an object, you can do to a function (including print them, as in your example); they are objects.

Answered By: Rick

When you do print string_function you aren’t actually calling the function. That command simply prints the representation of the function object itself. It’s a short-cut for doing print repr(string_function); in the interpreter simply doing string_function will print the same thing.

The 0x7f5111010938 in the representation is the id of the function object; that number will change every time you run the script. In standard Python (aka CPython) the id is the memory address of the object, but that’s just an implementation detail of CPython, it’s not necessarily the case in other flavours of Python. Also see https://docs.python.org/2/reference/datamodel.html

I don’t understand why you get None printed at the end, though.

Answered By: PM 2Ring