How do i use a global var in python?
class CompareRectangles(object):
debugging = True
def globals():
global debugging
debugging = True
def __init__(self,r1,r2):
# globals()
self.r1 = r1
self.r2 = r2
self.initialise_boundary_tests()
def method(self):
if debugging:
print("hello debugger")
compare_rects = CompareRectangles(r1,r2) NameError: global name 'debugging' is not defined
That's not a global, that's a class variable. You can access it by self.debugging
. I don't understand why you need the globals()
method.
class CompareRectangles(object):
debugging = True
def __init__(self,r1,r2):
self.r1 = r1
self.r2 = r2
self.initialise_boundary_tests()
def method(self):
if self.debugging:
print("hello debugger")
Really close. You need to add self
Change
def method(self):
if debugging:
print 'hello debugger'
to
def method(self):
if self.debugging: <-- notice the self
print 'hello debugger'
Code to explain your example :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.