use*_*121 2 python class global-variables
在Python类中,我有以下方法
def foo(self,arg1, arg2):
global run
run = False
Run Code Online (Sandbox Code Playgroud)
在同一个类中,在我的构造函数中
while run:
sleep(0.01)
self.each_frame()
self.server.close()
run = True
Run Code Online (Sandbox Code Playgroud)
以这种方式在类方法中精细化全局变量是否可以?
Mar*_*cin 10
这是一个可怕的想法.如果需要在实例之间共享的变量,请在类中定义它.如果需要在方法之间共享变量,请在对象上定义它们:
class Foo(object):
this_is_a_class_variable = 0
def __init__(self,*args):
self.this_is_an_object_variable = None
Run Code Online (Sandbox Code Playgroud)