Professional Documents
Culture Documents
Python is an object-oriented programming language. This means that almost all the code is
implemented using a special construct called classes. A class is a code template for creating
objects.
Table of contents
What is a Class and Objects in Python?
Create a Class in Python
Create Object of a Class
Class Attributes
Class Methods
Class Naming Convention
pass Statement in Class
Object Properties
Modify Object Properties
Delete object properties
Delete Objects
For example, If we design a class based on the states and behaviors of a Person, then States can
be represented as instance variables and behaviors as class methods.
Class: Person
Object 1: Jessa
State:
Name: Jessa
Sex: Female
Profession: Software Engineer
Behavior:
Working: She is working as Software developer in ABC Company
Study: She study 2 hours a day
Object 2: Jon
State:
Name: Jon
Sex: Male
Profession: Doctor
Behavior:
Working: He is working as Doctor
Study: She study 5 hours a day
As you can see, Jessa is female, and she works as a Software engineer. On the other hand, Jon is
a male, and he is a lawyer. Here, both objects are created from the same class, but they have
different states and behaviors.
Syntax
class class_name:
'''This is a docstring. I have created a new class'''
<statement 1>
<statement 2>
.
.
<statement N>
In this example, we are creating a Person Class with name, sex, and profession instance
variables.
class Person:
def __init__(self, name, sex, profession):
# data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession
A constructor is a special method used to create and initialize an object of a class. This method is
defined in the class.
In Python, Object creation is divided into two parts in Object Creation and Object
initialization
<object-name> = <class-name>(<arguments>)
class Person:
def __init__(self, name, sex, profession):
# data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession
# call methods
jessa.show()
jessa.work()
Run
Output:
Instance variables: The instance variables are attributes attached to an instance of a class.
We define instance variables in the constructor ( the __init__() method of a class).
Class Variables: A class variable is a variable that is declared inside of class, but outside
of any instance method or __init__() method.
All instances of a class share the class variables. However, unlike instance variables, the value of
a class variable is not varied from object to object.
Only one copy of the static variable will be created and shared between all objects of the class.
Accessing properties and assigning values
class Student:
# class variables
school_name = 'ABC School'
# constructor
def __init__(self, name, age):
# instance variables
self.name = name
self.age = age
s1 = Student("Harry", 12)
# access instance variables
print('Student:', s1.name, s1.age)
Run
Output
Student: Harry 12
Student: Jessa 14
Instance method: Used to access or modify the object state. If we use instance
variables inside a method, such methods are called instance methods.
Class method: Used to access or modify the class state. In method implementation, if we
use only class variables, then such type of methods we should declare as a class method.
Static method: It is a general utility method that performs a task in isolation. Inside this
method, we don’t use instance or class variable because this static method doesn’t have
access to the class attributes.
A class method is bound to the class and not the object of the class. It can access only class
variables.
Example: Define and call an instance method and class method
# constructor
def __init__(self, name, age):
# instance variables
self.name = name
self.age = age
# instance method
def show(self):
# access instance variables and class variables
print('Student:', self.name, self.age, Student.school_name)
# instance method
def change_age(self, new_age):
# modify instance variable
self.age = new_age
# class method
@classmethod
def modify_school_name(cls, new_name):
# modify class variable
cls.school_name = new_name
s1 = Student("Harry", 12)
Run
Output
We should follow specific rules while we are deciding a name for the class in Python.
The pass statement is used to have an empty block in a code because the empty code is not
allowed in loops, function definition, class definition. Thus, the pass statement will results in no
operation (NOP). Generally, we use it as a placeholder when we do not know what code to write
or add code in a future release.
For example, suppose we have a class that is not implemented yet, but we want to implement it
in the future, and they cannot have an empty body because the interpreter gives an error. So use
the pass statement to construct a body that does nothing.
Example
class Demo:
pass
In the above example, we defined class without a body. To avoid errors while executing it, we
added the pass statement in the class body.
Object Properties
Every object has properties with it. In other words, we can say that object property is an
association between name and value.
For example, a car is an object, and its properties are car color, sunroof, price, manufacture,
model, engine, and so on. Here, color is the name and red is the value. Object properties are
nothing but instance variables.
Object Properties
Obj.PROPERTY = value
Run
Example
class Fruit:
def __init__(self, name, color):
self.name = name
self.color = color
def show(self):
print("Fruit is", self.name, "and Color is", self.color)
Run
class Fruit:
def __init__(self, name, color):
self.name = name
self.color = color
def show(self):
print("Fruit is", self.name, "and Color is", self.color)
Run
In the above example, As we can see, the attribute name has been deleted when we try to print or
access that attribute gets an error message.
Delete Objects
In Python, we can also delete the object by using a del keyword. An object can be anything like,
class object, list, tuple, set, etc.
Syntax
del object_name
class Employee:
depatment = "IT"
def show(self):
print("Department is ", self.depatment)
emp = Employee()
emp.show()
# delete object
del emp
Run
In the above example, we create the object emp of the class Employee. After that, using
the del keyword, we deleted that object.