Python打印功能在解释器上获得意外输出?

Mar*_*ace 3 python

我在名为seta.py的文件中有以下内容:

def print_name():
    print "hello"
Run Code Online (Sandbox Code Playgroud)

我正在从口译员那里做以下事情:

import seta
Run Code Online (Sandbox Code Playgroud)

然后

seta.print_name
Run Code Online (Sandbox Code Playgroud)

我希望输出是"你好",但它如下:

<function print_name at 0x7faffa1585f0>

我究竟做错了什么?

Ash*_*ary 7

要调用函数,您需要添加():

seta.print_name()
Run Code Online (Sandbox Code Playgroud)

否则它将打印该功能对象的str/ repr版本.

演示:

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!
Run Code Online (Sandbox Code Playgroud)