Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python printing function gets unexpected output at interpreter?

Tags:

python

I have the following in a file named seta.py:

def print_name():
    print "hello"

I am doing the following from the interpreter:

import seta

and then

seta.print_name

I would expect the output to be "hello" but it is as follows:

<function print_name at 0x7faffa1585f0>

What am i doing wrong?

like image 693
Marty Wallace Avatar asked Jan 24 '26 04:01

Marty Wallace


1 Answers

To call a function you need to add ():

seta.print_name()

Otherwise it'll print the str/repr version of that function object.

Demo:

def func():
    print "Hello, World!"
>>> func                         #Returns the repr version of function object
<function func at 0xb743cb54>
>>> repr(func)
'<function func at 0xb743cb54>'
>>> print func                   #Equivalent to `print str(func)`
<function func at 0xb743cb54>

>>> func()                       #Eureka!
Hello, World!
like image 187
Ashwini Chaudhary Avatar answered Jan 26 '26 18:01

Ashwini Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!