Dna*_*iel 10 python global-variables
这个问题很普遍但是我更了解python中的良好编码实践...
我想在任何函数中定义一个我可以在其中使用的常量,而不必将其作为参数传递,并且无需在声明之后在任何地方更改它 - 也许它很难让它无法改变但不是确定我能做到这一点.
在编程风格(编码,名称约定等)方面,最好的方法是什么?
即,GLOBAL_CONSTANT ='this_variable_will_not_be_changed'
我的python代码会有这种形式,它也是好的风格吗?
#!/usr/bin/env python
import sys
import os
GLOBAL_CONSTANT='this_variable_will_not_be_changed'
def test():
print GLOBAL_CONSTANT
return 0
def main():
test()
return 0
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)