In Python Variable are three type

  • Local Variable
  • Class Variable (Advance Python Topic)
  • Instance Variable (Advance Python Topic)
  • Global Variable

Local Variable : In python if you define a variable inside constructor or method this is called Local variable

NOTE 1 : : method and constructor args is also called Local variable

NOTE 2 : : Local Variable Scope inside method and constructor only

NOTE 3 : : Local Variable Memory allocate when method and constructor class

class LocalVariable:
	def __init__(self,fname):
		lname="xyz" #Local Variable
		print(fname+" "+lname)
e1=LocalVariable("abc")
abc xyz

Class Variable : In python if you define a variable inside class but out side method and constructor is called class variable

NOTE 1 : class variable access with class name and self keyword

NOTE 2 : class Variable Scope inside class only

NOTE 3 : It recmmonded to access the class variable with class name

NOTE 3 : : Class Variable Memory allocate when dot pyc file loaded

class ClassVariable:

	i=1 #class variable

	def __init__(self):
		print(ClassVariable.i,"  ",self.i)
	
	def getMethod(self):	
		print(ClassVariable.i,"  ",self.i)

e1=ClassVariable().getMethod()

Instance Variable :In python if you define a variable inside method and constructor using self keyword is called Instance Variable

NOTE 1 : Instance Variable Scope inside class only

NOTE 2 : Instance Variable Memory allocate when object create

class InstanceVariable:

	def __init__(self,i):
		self.i=i # instance variable

	def getMethod(self):	
		print(self.i)

e1=InstanceVariable(10).getMethod()
10

Global Variable : In python if you define a variable outside class is called globale variable

NOTE 1 : is side function to declear global variable using global keyword

NOTE 2 : global variable direct access by name

NOTE 3 : global variable Scope in dot py file

NOTE 4 : inside the method once we are using global variable, in this case with the same name it is not possible to declare Local variable

abc=100
class InstanceVariable:

	def __init__(self):
		print(abc)

	def getMethod(self):	
		print(abc)

e1=InstanceVariable().getMethod()
print(abc)
100
100
100
comName="XYZ"
localtion="UP"
class B10:
	def __init__(self):
		comName="ABC"
		localtion="Pune"
		print(comName,"  ",localtion)
b10=B10()
ABC    Pune

CASE 1 :

comName="XYZ"
localtion="UP"
class B10:
	def __init__(self):
		print(comName,"  ",localtion)
		comName="ABC"
		localtion="Pune"
		print(comName,"  ",localtion)
b10=B10()
UnboundLocalError: local variable 'comName' referenced before assignment
i=10
class B10:
	def __init__(self):
		i=i+20
		print(i)			
b10=B10()
UnboundLocalError: local variable 'i' referenced before assignment

CASE 1 :

In side function or constructor to repersent the global variable using global keyword

comName="XYZ"
localtion="UP"
class B10:
	def __init__(self):
		global comName,localtion
		print(comName,"  ",localtion)
		comName="ABC"
		localtion="Pune"
		print(comName,"  ",localtion)

b10=B10()
print(comName,"  ",localtion)
XYZ    UP
ABC    Pune
ABC    Pune

in side the inner to repersent outer function variable using localvariable

def outerFunction():
	name="Ashu"
	def innerFunction():
		nonlocal name
		name="soonu"
	print(name)
	innerFunction()
	print(name)
outerFunction()		
Ashu
soonu

python global vs nonlocal keyword

name="ABC"
def outerFunction():
	fname="Ashu"
	def innerFunction():
		nonlocal fname
		fname="soonu"
		global name
		name="XYZ"

	print(name)
	innerFunction()
	print(name)

outerFunction()
print(name)
ABC
XYZ
XYZ

All Variable with same name in python

comName="XYZ"
localtion="UP"
class B10:

	comName="APP"
	localtion="California"
	
	def __init__(self,comName,localtion):
			self.comName=comName
			self.localtion=localtion

	def displayAllVaraible(self):

		comName="ABC"
		localtion="pune"

		#as local variable
		print(comName+" "+localtion)

		#instance variable
		print(self.comName+" "+self.localtion)
		
		#class variable comName and localtion
		print(B10.comName+" "+B10.localtion)
		
		#as globals variable
		print(globals()['comName']+" "+globals()['localtion'])
		
b10=B10("JBBAPP","Germany")
b10.displayAllVaraible()
ABC pune
JBBAPP Germany
APP California
XYZ UP