Skip to main content

Classes in Python

Python is an object-oriented programming language and classes are the fundamental building blocks for creating objects. This cheat sheet will walk you through all the basics of classes in Python.

Creating a Class

In Python, we create a class using the class keyword. The name of the class usually follows CamelCase convention.

class MyClass:
x = 5

In this example, we've created a class named MyClass with a single attribute x. You can create an object of this class and access its attribute using dot notation.

Instances

An instance is a specific object created from a particular class. You can create an instance of a class by calling the class name as if it were a function.

p1 = MyClass()
print(p1.x)

In this snippet, p1 is an instance of MyClass. We're printing the attribute x of this instance, which should output 5.

__init__() Function

The __init__() function in Python is a special method that's automatically called when an object of a class is instantiated. It's used to initialize the attributes of the class.

class MyClass:
def __init__(self, name):
self.name = name

p1 = MyClass("John")
print(p1.name)

In this example, __init__() is used to assign a name to the instance at the time of creation. We're passing "John" as a parameter while creating p1, which is then accessible as an attribute of the instance.

Methods

Methods are functions defined inside the body of a class. They are used to perform operations that sometimes utilize the attributes of the class.

class MyClass:
def __init__(self, name):
self.name = name

def greet(self):
print(f"Hello, {self.name}!")

p1 = MyClass("John")
p1.greet()

In this code, we've added a method greet to our class that prints a greeting message. We're calling this method on the p1 instance, which should print "Hello, John!".

self Parameter

The self parameter in Python is a reference to the current instance of the class. It is used to access variables and methods associated with the class.

class MyClass:
def __init__(self, name):
self.name = name

def change_name(self, new_name):
self.name = new_name

p1 = MyClass("John")
p1.change_name("Jane")
print(p1.name)

In this snippet, the change_name method uses the self parameter to modify the name attribute of the instance. After calling this method on p1, printing p1.name should output "Jane".

Modifying Objects

You can modify the attributes of an object directly, or by defining methods for the purpose.

p1.name = "Alice"
print(p1.name)

In this example, we've directly changed the name attribute of p1 to "Alice". Printing p1.name should now output "Alice".

Inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class. The class from which we're inheriting is called the parent class, and the class that is inherited is called the child class.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def greet(self):
print(f"Hello, my name is {self.name}.")

class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade

def study(self):
print(f"I'm studying in grade {self.grade}.")

s1 = Student("Alice", 12, 6)
s1.greet()
s1.study()

In this example, we have a parent class Person and a child class Student. The Student class inherits the attributes and methods of the Person class, demonstrated by the usage of the super() function. We also add a new attribute grade and a new method study to the Student class. When we create an instance of Student and call the greet and study methods, it should print "Hello, my name is Alice." and "I'm studying in grade 6.", respectively.