如何在 python 类中声明全局变量。我的意思是:如果我有这样的类(class)
class HomePage(webapp2.RequestHandler):
def get(self):
myvaraible = "content"
#do something
class UserPage(webapp2.RequestHandler):
#i want to access myvariable in this class
最佳答案
你也可以为此使用类变量,这是比全局更简洁的解决方案:
class HomePage(webapp2.RequestHandler):
myvariable = ""
def get(self):
HomePage.myvariable = "content"
您也可以从其他类再次使用 HomePage.myvariable
访问它。
要创建一个普通的实例变量,使用这个:
class HomePage(webapp2.RequestHandler):
def get(self):
self.myvariable = "content"
关于python - 在 Python 类中声明全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20951130/