时,将不会再重新求值。
这是一个新手问题,我很确定这个函数只会执行一次,但我想清楚。我使用python和urllib登录到一个网站。一旦登录,有一个用户id与我的帐户相关联,这将允许我浏览网站。然而,我不想登录与每次调用变量,所以我想知道是否调用变量会导致再次登录,或者如果登录函数只执行,如果我显式运行我的登录函数?
def login(username,pw):
#some calls to HTTP server using urllib
return user_id
user_id = login('abc@hotmail.com','mypassword')
def search():
#calls to HTTP to perform a search on a page on the website
#I need to reference user_id and I want to know if user_id will simply be
# the integer user_id or if it will call the login function each time I reference it
像刚才那样赋值一个变量,立即且只计算右侧部分的值一次。当使用变量
引用user_id
将只返回您分配给它的原始值;login()
函数不会被调用