Menu Close

Python class and object

Python Class and Object

In This Python OOPs tutorial, you will learn all about Python classes and objects along with examples. class and object are the most important part of Python Object-Oriented Programming.
In this guide, we will see how to create a class, and also we will see how to create more than one object using created class.

In previous tutorials, we have seen all about Python Object Oriented Programming concepts along with examples.

What is Python class and object?

Python is an Object-Oriented Programming language almost everything in python is an object. In python, the object is a collection of properties and methods that act on those data.

In Python, a class is a blueprint for creating objects.

Let’s understand the Python class and object with an appropriate example.

A class is a sketch (Prototype) of a house. It contains a description of all the floors, rooms, doors, windows, etc.
Based on this sketch of a house or prototype we can create a house. so here house is an object.
With the help of a house prototype, we can build more than one house which means we can create more than one object with the help of the class.

Define a class in Python

In Python to create a class, use the class keyword.

class myClass:
	first_name = 'Programming'
	last_name = 'Funda'
	
	def fullName(self):
		print(f"My full name is {self.first_name + ' ' + self.last_name}.")

In the above example, myClass is the name of the class and first_name and last_name are the properties of the class and fullName is the method of the class.

Create an object

To create a Python object of the class, use the class name with parentheses.

class myClass:
	first_name = 'Programming'
	last_name = 'Funda'
	
	def fullName(self):
		print(f"My full name is {self.first_name + ' ' + self.last_name}.")
	
obj = myClass()
obj.fullName()
print(obj.first_name)
print(obj.last_name)

Docstring of class

Docstring is documentation of the class which tells about the class.
In python, __doc__ is a special attribute that is used to access the docstring of the class.

class Profile:
	"This is demo class example"
	name = 'John'
	age = 21
	

obj = Profile()
print(obj.name,obj.age)
print(Profile.__doc__)	

Output

John 21
This is demo class example

Constructors in Python

A class function that begins with a double underscore (__) is called a special method which is also called a constructor.

All classes have function init(), which is automatically called when the object is instantiated.
With the help of the __init__() function, you can assign the value to object properties.
create a class Profile, and use the __doc__() function to assign the name and age.

class myClass:
	def __init__(self, firstName, lastName):
	
		self.first_name = firstName
		self.last_name = lastName
	
	def fullName(self):
		print(f"My full name is {self.first_name + ' ' + self.last_name}.")


p1 = myClass('Vishvajit', 'Rao')
p1.fullName()
print(p1.first_name)
print(p1.last_name)

Output

My full name is Vishvajit Rao.
Vishvajit
Rao

The self Parameter

The self parameter is refer to the current instance of the class, and it is used to access to the variable that belongs to the class. It does not name self, you can change it whatever you like but it should be the first parameter of the function in the class.

Example:

class Person:
	x = 'Python Programming'
	def name(abc):
		print(abc.x)
		
		
p1 = Person()
p1.name()

Output

Python Programming

Object Methods

The object can also contain methods. A method is the class method that belongs to an object.

Example

Create a class Person and also create function details that print the name and age of the person.

class Person:
	def __init__(self,name,age):
		self.name = name
		self.age = age
		
	def details(self):
		print(self.name,self.age)
		
		
#Create p1 object-		
p1 = Person('Maya', 23)
p1.details()

#Create another p2 object-
p2 = Person('Mariya', 33)
p2.details()

Output

Maya 23
Mariya 33

Modify object properties

You can change the value of the properties of the class using the object.

class Person:
	def __init__(self,name,age):
		self.name = name
		self.age = age
		
	def details(self):
		print(self.name)	
p1 = Person('Maya', 23)
p1.details()
p1.age = 40
print(p1.age)

Output

Maya
40

Delete object property

You can delete properties on objects by using the del keyword.

class Person:
	def __init__(self,name,age):
		self.name = name
		self.age = age
		
	def details(self):
		print(self.name)
				
p1 = Person('Maya', 23)
del p1.age
print(p1.age)

Output

Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 13, in <module>
    print(p1.age)
AttributeError: 'Person' object has no attribute 'age'

Delete Object

You can delete objects by using the del keyword.

Example:

class Person:
	def __init__(self,name,age):
		self.name = name
		self.age = age
	def details(self):
		print(self.name)
				
p1 = Person('Maya', 23)
del p1
print(p1.details)

Output

Traceback (most recent call last):
  File "C:\Users\Vishvajit\Desktop\PF Article\Python Basic part1\test.py", line 10, in <module>
    print(p1.details)
NameError: name 'p1' is not defined

The pass statement

In Python class and object class can not be empty, but sometimes if you want to use an empty class then you can use a pass statement. Basically pass statement is a null statement.

Example:

class Person:
	pass

Conclusion

In this guide, you have learned all about Python class and object along with appropriate examples.
If you want to learn Python programming, Then you don’t ignore Python class and object. Using Python class and object you can solve any programming problem easily.

If you like this article, please share and keep visiting for further Python tutorials.

For more reference:- Click Here

Frequently Asked Question About Python class and object.


  1. What is Python class and object?

    Ans:- Python is Object Oriented Programming language. Almost everything in Python is an object, with its properties and methods. A class is an object constructor or blueprint for creating an object.

  2. What is the difference between Python class and object?

    Ans:- A object is simply a collection of properties and methods. Class is a blueprint for creating an object.

  3. What is __init__ in Python?

    Ans:- In Python, __init__ is a special method or magic method. It is also called Constructor. Python __init__ method is called automatically when the object is instantiated.

  4. How do you create an object of the class in Python?

    Ans:- To create the object of the class, use the class name follows by the parentheses.

  5. What type of language is Python?

    Ans:- Python is interpreted, an object-oriented, high-level programming language with dynamic semantics.

Happy Coding
~ Vishvajit Rao

Python Dictionary Tutorial
Python Collections Module ( With Examples )

Related Posts