use*_*422 18 python function global-variables
是否有一种简单的方法可以使函数中的所有变量全局化?
我在一个函数中有20个奇数变量并且逐个命名它们并不是很好的代码...反正我:)
mha*_*wke 50
在正常的日常编程过程中没有正当理由做以下事情.请查看此问题的其他答案,以获得更实际的替代方案.
我几乎无法想象你为什么要这样做,但这是一种方法:
def f(a, b, c):
d = 123
e = 'crazy, but possible'
globals().update(locals())
def g():
print a, b, c, d ,e
>>> globals()
{'g': <function g at 0x875230>, 'f': <function f at 0x8751b8>, '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', '__doc__': None}
>>> g()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in g
NameError: global name 'a' is not defined
>>> f(10, 20, 'blah')
>>> g()
10 20 blah 123 crazy, but possible
>>> globals()
{'a': 10, 'c': 'blah', 'b': 20, 'e': 'crazy, but possible', 'd': 123, 'g': <function g at 0x875230>, 'f': <function f at 0x8751b8>, '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, '__name__': '__main__', '__doc__': None}
Run Code Online (Sandbox Code Playgroud)
Ned*_*der 13
没有办法将它们全部声明为全局,你真的不想这样做.这20个变量可能应该变成具有20个属性的对象.
sni*_*im2 13
执行此操作的pythonic方法是将变量保持在局部范围内(即在每个函数中定义它们)并将它们作为参数/返回值在函数之间传递; 或者将变量保存为在该类中创建"函数"方法的对象或类的属性.无论哪种方式都可以,但global关键字专门设计用于让您以您描述的方式使用它.全局变量不仅仅是"坏的风格",而且它们使代码很难维护,因为需要在每个函数中检查变量需要遵守的任何不变量.
这是一个好样式的例子(带有函数):
def quads(a, b, c):
x1 = (-1.0 * b + math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a)
x2 = (-1.0 * b - math.sqrt(b * b - 4.0 * a * c)) / (2.0 * a)
return x1, x2
def pretty(a, b, c, x1, x2):
eqn = "%fx^2 + %fx + %c" % (a, b, c)
print "The first solution to the equation %s is: %f" % (eqn, x1)
print "The second solution to the equation %s is: %f" % (eqn, x2)
return
def main():
a = 100
b = 200
c = 300
x1, x2 = quads(a, b, c)
pretty(a, b, c, x1, x2)
return
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
这是一个好样式的例子(使用OOP):
class Quadratic(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
self.x1 = None
self.x2 = None
self.solve() # Set x1 and x2 to correct values
# To maintain the invariant between a, b, c and x1, x1
# we should override __setattr__ or use descriptors or
# properties so that self.solve() is called every time
# a, b, or c are updated.
return
def solve(self):
self.x1 = (-1.0 * self.b +
math.sqrt(self.b * self.b - 4.0 * self.a * self.c)) / (2.0 * self.a)
self.x2 = (-1.0 * self.b -
math.sqrt(self.b * self.b - 4.0 * self.a * self.c)) / 2.0 * self.a
return
def pretty(self):
eqn = "%fx^2 + %fx + %c" % (self.a, self.b, self.c)
print "The first solution to the equation %s is: %f" % (eqn, self.x1)
print "The second solution to the equation %s is: %f" % (eqn, self.x2)
return
def main():
quad = Quadratic(100, 200, 300)
quad.pretty()
return
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
最简单的解决方案是只有一个全局变量——或者更好的是,弄清楚如何将它传递给函数。将它用作全局变量看起来像这样(同样,我展示了最简单的可能情况,不一定是 Python 的最佳用法):
class Info(object): # or whatever you want to name the container
"""Holder for global information."""
info = Info() # single instance we will use
def my_function():
print "Here is some info:"
print info.a, info.b, info.c
info.a = 3
info.b = 8
info.c = []
if __name__ == '__main__':
my_function()
Run Code Online (Sandbox Code Playgroud)
同样,我可能会info改为传递给函数。但是由于您的问题是关于全局的,因此这里显示为全局。