Class vs Instance Variables

Class Variables

Class variables are shared among all instances of a class. They are associated with the class rather than any particular instance.

class ClassName:
	''' Optional docstring '''
	# Class variables defined here
	class_var1 = value1
	...
	
	def __init__(self, attribute1, ...):
		self.attribute1 = attribute1
		...
	...

Instance Variables

Instance variables are specific to each instance of a class. Each instance has its own set of instance variables.

class ClassName:
	''' Optional docstring '''
	# Class variables defined here
	class_var1 = value1
	...
	
	def __init__(self, attribute1, ...): # defining instance variables
		self.attribute1 = attribute1 
		...
	...
Powered by Forestry.md