Creating Class in Python
Creating Class in Python
Introduction
In Python, a class is a blueprint or template for creating objects. It defines the structure and
behavior of those objects. This guide will walk you through creating a simple class, defining its
public interface, and understanding constructors.
To create a class in Python, you use the class keyword, followed by the class name. A class
typically contains attributes (data) and methods (functions) that operate on that data. Here's a
basic class structure:
python
class ClassName:
def __init__(self):
# Constructor (optional)
self.attribute1 = value1
self.attribute2 = value2
def method1(self):
# Method 1
# Access and manipulate attributes
pass
def method2(self):
# Method 2
pass
Constructors
A constructor is a special method in a class that is automatically called when an object of the
class is created. It is used to initialize attributes or perform any setup required for the object. In
Python, the constructor method is named __init__ and is always the first method to be called
when an object is created.
python
class MyClass:
def __init__(self, param1, param2):
self.attribute1 = param1
self.attribute2 = param2
Example
Let's implement a simple class called Person to understand the concepts discussed above:
python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def say_hello(self):
return f"Hello, my name is {self.name} and I'm {self.age} years old."
1. Class: A class is a blueprint or template for creating objects. It defines the structure and
behavior that the objects of the class will have. It's like a recipe for creating objects with
specific attributes and methods.
2. Constructor: A constructor is a special method in a class that is automatically called
when you create an object (instance) of that class. In Python, the constructor method is
named __init__. It is used to initialize the object's attributes, setting their initial values.
3. Object (Instance): An object, also known as an instance, is a specific individual entity
created from a class. Objects are concrete instances of a class, and they have their own set
of attributes and can perform actions defined by the class's methods.
● Class is the blueprint, defining what attributes and methods an object will have.
● Object (Instance) is the actual, concrete instance created from the class, with its specific
attribute values as defined by the constructor.
python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
In this example:
● Person is the class, defining the structure of a person with attributes name and age.
● __init__ is the constructor method, initializing the name and age attributes when
creating a Person object.
● alice and bob are objects (instances) of the Person class, each with its unique name and
age attributes.
Let's explore a few more examples to illustrate the concepts of creating classes, defining public
interfaces, and using constructors in Python.
def start(self):
print(f"{self.year} {self.make} {self.model} is starting.")
def perimeter(self):
return 2 * (self.width + self.height)
def average_grade(self):
if len(self.grades) == 0:
return 0
return sum(self.grades) / len(self.grades)
# Adding grades
student1.add_grade(85)
student1.add_grade(90)
student2.add_grade(78)
student2.add_grade(92)
def display_info(self):
print(f"Title: {self.title}")
print(f"Author: {self.author}")
print(f"ISBN: {self.ISBN}")
def introduce(self):
print(f"Hi, I'm {self.name} and I'm {self.age} years old.")
class Employee(Person):
def __init__(self, name, age, employee_id):
super().__init__(name, age)
self.employee_id = employee_id
def introduce(self):
print(f"Hello, I'm {self.name}, {self.age} years old, and my employee
ID is {self.employee_id}.")
# Introduce themselves
person1.introduce()
employee1.introduce()
These examples demonstrate how to create classes for various real-world scenarios, define their
public interfaces, and utilize constructors to initialize objects and their attributes.
7. Conclusion
Creating classes in Python is a fundamental concept in object-oriented programming.
Understanding the public interface and constructors is essential for designing effective classes.
You can use these classes to model real-world entities and encapsulate data and behavior within
them.