def hh():
print(a)
if __name__ == '__main__':
a=20
hh()
20
和js差不多的一个道理,有一个 LEGB 变量作用域,js里面是什么作用域链,但c和c++似乎不是这样(我好久没复习了)
使用场景
为什么想到这个是因为,我在接手师兄的项目时候,注册界面是登录界面的一个子界面(都在一个窗口),我要求在用户点击进入注册时,窗口名字应该改为 账号注册
, 注册完(或者返回)的时候跳回登录界面,窗口名字应该又改为 登陆界面
def show_register():
print("展示注册界面") # 进入注册界面
login_window.setWindowTitle('账号注册') # login_window是登录界面,是我在main 函数里面定义的
animation = QPropertyAnimation(register_vb)
animation.setTargetObject(register_vb)
animation.setPropertyName(b"pos")
animation.setStartValue(register_vb.pos())
animation.setEndValue(QPoint(0, 0))
animation.setDuration(1000)
animation.setEasingCurve(QEasingCurve.OutBounce)
animation.start(QAbstractAnimation.DeleteWhenStopped)
login_window.show_register_signal.connect(show_register)
def exit_register():
print("退出注册页面")
login_window.setWindowTitle('登陆界面')
animation = QPropertyAnimation(register_vb)
animation.setTargetObject(register_vb)
animation.setPropertyName(b"pos")
animation.setStartValue(QPoint(0, 0))
animation.setEndValue(QPoint(0,login_window.width()))
animation.setDuration(1000)
animation.setEasingCurve(QEasingCurve.OutBounce)
animation.start(QAbstractAnimation.DeleteWhenStopped)
register_vb.exit_signal.connect(exit_register)
随手记录下,加深印象,主要是习惯了传参,返回结果的模式,哈哈哈哈,一时间没想到。有一说一,虽然有这样的保护机制,但是我还是觉得变量不要起相同名字,不太好阅读,计算机是知道,但是看代码的人不晓得,也可能是我太菜,哈哈哈哈,个人习惯
这篇文章讲的错 Python变量作用域及闭包
更多推荐
python的变量作用域,main函数里面的变量,其他函数里面也能直接用
发布评论