# File function2.py
def f():
print("I am f")
return("return f")
if __name__ == "__main__":
print("print(f) gives", end=": "); print(f)
print("f gives", end=": "); f
print("f() gives", end=": "); f()
如果您运行代码python function2.py
,则会有
print(f) gives: <function f at 0x026CD6F0>
f gives: f() gives: I am f
但是,如果以交互方式键入并执行代码,您将拥有:
>>> def f():
... print("I am f")
... return("return f")
...
>>> if __name__ == "__main__":
... print("print(f) gives", end=": "); print(f)
... print("f gives", end=": "); f
... print("f() gives", end=": "); f()
...
print(f) gives: <function f at 0x025BD6F0>
f gives: <function f at 0x025BD6F0>
f() gives: I am f
'Return f'
>>>
这是一个错误吗?
答案 0 :(得分:1)
@DSM是对的。 ;
背后的东西是一个新的陈述。
你写的是
print("print(f) gives", end=": ")
print(f)
print("f gives", end=": ")
f # this causes the program to just go over it and do nothing but in the REPL this is printing f
print("f() gives", end=": ")
f()
REPL =阅读评估Print Loop =交互式解释器= >>>
所以你做了
>>> f # print(f) in REPL
<function f at 0x025BD6F0>
>>> f() # print(f()) in REPL # = print("I am f"); print("return f")
I am f
'return f'
因此,REPL在程序中输入的每一行都附加print()
。
这就是输出的原因。如果您想让它保持一致,请使用print
。