How do i declare global variable in python class. What i mean is : if i have a class like this
class HomePage(webapp2.RequestHandler):
def get(self):
myvaraible = "content"
#do something
class UserPage(webapp2.RequestHandler):
#i want to access myvariable in this class
You can also use class variables for this, which are a bit cleaner solution than globals:
class HomePage(webapp2.RequestHandler):
myvariable = ""
def get(self):
HomePage.myvariable = "content"
You can access it with HomePage.myvariable
again from other classes also.
To create an ordinary instance variable use this:
class HomePage(webapp2.RequestHandler):
def get(self):
self.myvariable = "content"
Declare the variable global wherever you assign it
myvariable = None
class HomePage(webapp2.RequestHandler):
def get(self):
global myvariable
myvariable = "content"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With