Loading...

Python Class and Objects


in this tutorial we are going to learn what are classes and objects in python, what are attributes and methods in python, how to create a class and object, constructor in python, self keyword in python, pass statement in class in python


What are class and objects in python ?


python is an object oriented programming language and objects are based on classes.
class
a class is a template or blueprint which defines a collection of attributes and methods under a one common name.
it does not occupy any memory and is not tangible. it is used for creating objects which occupies memory.
for example: a class may be a Product (represents blueprint for products)
objects
an object is an instance of a class which represents the real entity.
for example: an object may be an 'iphone' (instance of class Product)

What are attributes and methods in python ?


python is an object oriented programming language and objects are based on classes.
attributes
in a class the collection of variables are called attributes.
they are defined and declared typically (conventioally) at the top.
for example:
        		     					    		
class Mobile:
	name = 'iphone 9'
	price = '$ 399'
	description = 1
        		     					    		
        		     					    	
methods
functions in a class are called methods.
they are defined typically (conventionally) below the attributes.
for example:
        		     					    		
class Mobile:
	name = 'iphone 11 pro'
	price = '$ 1299'
	description = 1

	def getName(self, name):
		print(self.name)

	def getPrice(self, price):
		print(self.price)	

	def getDescription(self, description):
		print(self.description)

        		     					    		
        		     					    	


How to create a class in python ?


in python we create a class by using the class keyword.
for example:
        		     					    		
class Product:
	name = 'iphone 11'
	price = '$ 999'
	quantity = 1

        		     					    		
        		     					    	


How to create an object in python ?


in python we create a an object by using class name followed by parentheses and assign it to a variable.
for example:
        		     					    		
class Product:
	name = 'iphone 11'
	price = '$ 999'
	quantity = 1


obj = Product()

print("product is : " + obj.name)

output:

product is :iphone 11

        		     					    		
        		     					    	


Constructor in python


__init__() function is a constructor in python
it is always implicitly invoke before instantiating an object in python.
the constructor is used to initialize the object variables before creating an object.
for example:
        		     					    		
class Product:

	def __init__(self, name, price, quantity):
		self.name = name
		self.price = price
		self.quantity = quantity
        
	def showDetails(self):
		print("product is : " + self.name)
		print("price : " + self.price)
		print("qty is : ", self.quantity)
                
obj = Product('iphone 11', '$ 999', 1)

obj.showDetails()

output:

product is : iphone 11
price : $ 999
qty is :  1

        		     					    		
        		     					    	


What is self keyword in python ?


in python a self keyword refers to the current object of the class.
it is used to access the attributes on behalf of the current object in the class.
it is just a convention to named self but it is not compulsory you can name it whatever you want.

for example:
        		     					    		
class Product:
        # change self to myobj
	def __init__(myobj, name, price, quantity):
		myobj.name = name
		myobj.price = price
		myobj.quantity = quantity
        
	def showDetails(myobj):
		print("product is : " + myobj.name)
		print("price : " + myobj.price)
		print("qty is : ", myobj.quantity)
                
obj = Product('samsung s20', '$ 999', 1)

obj.showDetails()


output:

product is : samsung s20
price : $ 999
qty is :  1
        		     					    		
        		     					    	

in the the above example change the self keyword to myobj and then it runs perfectly fine.


The pass statement in class in python


just like we use pass statement in function we can alos use it in class definition.
in python we can not left a class blank on empty the interpreter will raise an error.
pass statement is used in that scenario to avoid these error in class definition.
it acts as a placeholder in class definition.
for example:
        		     					    		
class Product:
	pass