最近看代码,被变量赋值搞得头晕,略微整理一下。
#t.py
def hello():
print("hello")
return 1
#main1.py
from t import hello
hello=1 #hello指向1,从此开始,导入的hello函数丢失。
h=hello() #报错:'int' object is not callable
h=hello
print(h)
#main2.py
from t import hello
print(hello()) #打印:hello #此处调用了函数hello,然后打印返回值
# 1
h=hello() #打印:hello #此处h指向函数返回值
print(h) #打印:1
h=hello #此处h指向函数
print(h) #打印:<function hello at 0x10e898b70>
做整理的时候看到《流畅的python》说:普通对象可以通过实现实例变量__call__()来函数化调用,这部分内容后面有机会再细做说明吧。