# Consider the following class:
# Here constructor initializes variable_1 and variable_2. Both to 0
class localGlobal():
def __init__(self):
self.variable_1 = 0
variable_2 = 0
def setVar(self, var):
#set variable_1
self.variable_1 = var
variable_2 = var + 1
print "variable_2", variable_2
# Let us create a object of this class
obj = localGlobal()
#Now, variable_1 being a class variable (or class attribute),
#we can access it as follows
obj.variable_1
#Same is not true for variable_2. It will result in AttributeError.
#obj.variable_2
#Traceback (most recent call last):
# File "
#AttributeError: localGlobal instance has no attribute 'variable_2'
#The reason for the error is self explanatory. The 'variable_2' is
#not defined as a class attribute (not defined with self).
#it is just a local variable inside the 'init' and 'setVar'. If it is tried to access outside
# the class, it results in a NameError as shown below
variable_2
#Traceback (most recent call last):
# File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
# exec codeObject in __main__.__dict__
# File "C:\Work\MyWork\Python\Programs\local_global.py", line 36, in
# variable_2
#NameError: name 'variable_2' is not defined
#Now let us consider one more variable 'variable_3'
class localGlobal():
def __init__(self):
self.variable_1 = 0
variable_2 = 0
def setVar(self, var):
#set variable_1
self.variable_1 = var
variable_2 = var + 1
print "variable_2", variable_2
self.variable_3 = var + 2
# Let us create a object of this class
obj = localGlobal()
# If we try to access the 'variable_3', we'll see an AttributeError
obj.variable_3
##Traceback (most recent call last):
## File "
##AttributeError: localGlobal instance has no attribute 'variable_3'
##The reason for this error is that, the attribute 'variable_3' is not created yet.
##This variable is getting created in 'setVar' method. So, once the 'setVar' method
##is executed, 'variable_3' will be available
#call setVar method
obj.setVar(10)
#Now try to access the 'variable_3'. We'll get no error
obj.variable_3
#Now let us consider one more variable 'variable_4'
class localGlobal():
def __init__(self):
self.variable_1 = 0
variable_2 = 0
def setVar(self, var):
#set variable_1
self.variable_1 = var
variable_2 = var + 1
print "variable_2", variable_2
self.variable_3 = var + 2
variable_4 = var + 3
def getVar4(self):
print 'variable_4', variable_4
# Let us create a object of this class
obj = localGlobal()
##if we try to access the 'variable_4'(through 'getVar4') we will get the NameError.
##You may state the reason as 'variable_4' is not created yet because
##'setVar' is not executed. Let us try to execute 'setVar' and then access
##'variable_4'
obj.setVar(10)
obj.getVar4()
##We'll still see a name error as below. Becase, the name 'variable_4'
##is not a global name. In python, scope of all names defined inside a method
##is limited to that method.
##variable_4Traceback (most recent call last):
## File "
## File "C:\Work\MyWork\Python\Programs\local_global.py", line 87, in getVar4
## print 'variable_4', variable_4
##NameError: global name 'variable_4' is not defined
##Consider the below example. One 'variable_4' is defined outside class and another
##inside 'setVar' function. When this block of code is executed we see that different
##values of 'variable_4' are printed out.
##variable_4(1) 13
##variable_4(2) 12
##'variable_4' accessed inside 'setVar' is its local.
##'variable_4' accessed in 'getVar4' is the 'variable_4' defined outside class.
variable_4 = 12
class localGlobal():
def __init__(self):
self.variable_1 = 0
variable_2 = 0
def setVar(self, var):
#set variable_1
self.variable_1 = var
variable_2 = var + 1
print "variable_2", variable_2
self.variable_3 = var + 2
variable_4 = var + 3
print 'variable_4(1)', variable_4
def getVar4(self):
global variable_4
print 'variable_4(2)', variable_4
obj = localGlobal()
obj.setVar(10)
obj.getVar4()
Back to Python Home Page
No comments:
Post a Comment