Python Classes and Object Oriented Programming

Python classes can be defined using following syntax.

class ClassName:
    <statement-1>
    .
    .
    .
    <statement-N>

Below is a simple class which demonstrate an example to create class, create constructor, define method, define class variable, define instance variable and how to access them.

class Complex:

	#class variable, which is hared among all instance
	#access inside or outside class by class reference as Complex.count
	count = 0

	#constructor, use to create a new instance
	#instance variable can be add and accessed using self
	def __init__(self,x,y):
		self.x = x
		self.y = y
		Complex.count +=1

	#use of Class variable inside class
	def displayTotal(self):
		print("Total numbers are:"+str(Complex.count))
	
	#use of instace variable inside class
	def display(self):
		print(str(self.x)+"+"+str(self.y)+"i")


#create a new instance
c1 = Complex(4,5)

#call method
c1.display()

#create another instance
c2 = Complex(6,7)

Find detailed official documentation for python classed here.

Leave a Reply

Your email address will not be published. Required fields are marked *