admin管理员组文章数量:1794759
python的变量作用域,main函数里面的变量,其他函数里面也能直接用
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函数里面的变量,其他函数里面也能直接用 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686490382a73381.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论