Python Classes and Objects

Python Classes and Objects Tutorial

Welcome to this comprehensive tutorial on Python classes and objects, brought to you by codeswithpankaj.com. In this tutorial, we will explore the fundamental concepts of object-oriented programming in Python, focusing on classes and objects. By the end of this tutorial, you will have a solid understanding of how to define classes and create objects in Python.

Table of Contents

  1. Introduction to Object-Oriented Programming

  2. Defining a Class

  3. Creating Objects

  4. Class Attributes and Methods

  5. The __init__ Method

  6. Instance Attributes

  7. Instance Methods

  8. Practical Examples


1. Introduction to Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects," which can contain data and methods. OOP aims to implement real-world entities like inheritance, polymorphism, encapsulation, etc., in programming.

Why OOP is Important

OOP helps in:

  • Organizing code into reusable and logical structures

  • Encapsulating data and functionality together

  • Facilitating code maintenance and scalability

  • Enhancing code readability and manageability


2. Defining a Class

A class is a blueprint for creating objects. It defines a set of attributes and methods that the objects created from the class can use.

Syntax

class ClassName:
    # class attributes and methods

Example

# Defining a class
class Person:
    pass  # Placeholder for an empty class

# Creating an object
p = Person()
print(p)  # Output: <__main__.Person object at 0x7f4a4d2e3b20>

In this example, we define an empty class named Person using the class keyword. The pass statement is a placeholder indicating that the class has no content yet. We then create an object p from the Person class.


3. Creating Objects

Objects are instances of a class. You can create multiple objects from a single class.

Example

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

# Creating objects
person1 = Person("John", 25)
person2 = Person("Jane", 30)

print(person1.name, person1.age)  # Output: John 25
print(person2.name, person2.age)  # Output: Jane 30

In this example, we define a Person class with an __init__ method that initializes the name and age attributes. We then create two objects, person1 and person2, from the Person class, passing different values for the name and age attributes.


4. Class Attributes and Methods

Class attributes are variables that belong to the class. Methods are functions defined inside the class that describe the behaviors of the objects.

Example

class Person:
    species = "Homo sapiens"  # Class attribute

    def __init__(self, name, age):
        self.name = name  # Instance attribute
        self.age = age  # Instance attribute

    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# Creating an object
person1 = Person("John", 25)
person1.greet()  # Output: Hello, my name is John and I am 25 years old.
print(person1.species)  # Output: Homo sapiens

In this example, we define a Person class with a class attribute species and an instance method greet. The greet method prints a greeting message using the object's name and age attributes. We create an object person1 from the Person class and call the greet method.


5. The __init__ Method

The __init__ method is a special method called a constructor. It is automatically called when an object is created and is used to initialize the object's attributes.

Example

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

    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# Creating an object
person1 = Person("John", 25)
person1.greet()  # Output: Hello, my name is John and I am 25 years old.

In this example, the __init__ method initializes the name and age attributes for each object created from the Person class. When we create an object person1, the __init__ method is called automatically with the values "John" and 25 as arguments.


6. Instance Attributes

Instance attributes are variables that are specific to an object. They are defined within the __init__ method and prefixed with self.

Example

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

    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# Creating objects
person1 = Person("John", 25)
person2 = Person("Jane", 30)

print(person1.name, person1.age)  # Output: John 25
print(person2.name, person2.age)  # Output: Jane 30

In this example, we define instance attributes name and age within the __init__ method. These attributes are specific to each object created from the Person class.


7. Instance Methods

Instance methods are functions defined inside a class that operate on instances of the class. They are defined with self as their first parameter.

Example

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

    def greet(self):
        print(f"Hello, my name is {self.name} and I am {self.age} years old.")

# Creating an object
person1 = Person("John", 25)
person1.greet()  # Output: Hello, my name is John and I am 25 years old.

In this example, the greet method is an instance method that prints a greeting message using the object's name and age attributes. We create an object person1 from the Person class and call the greet method.


8. Practical Examples

Example 1: Bank Account Class

class BankAccount:
    def __init__(self, account_number, balance=0):
        self.account_number = account_number
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        print(f"Deposited ${amount}. New balance: ${self.balance}")

    def withdraw(self, amount):
        if amount > self.balance:
            print("Insufficient funds")
        else:
            self.balance -= amount
            print(f"Withdrew ${amount}. New balance: ${self.balance}")

# Creating an object
account = BankAccount("12345678", 1000)
account.deposit(500)  # Output: Deposited $500. New balance: $1500
account.withdraw(200)  # Output: Withdrew $200. New balance: $1300
account.withdraw(2000)  # Output: Insufficient funds

In this example, we define a BankAccount class with attributes account_number and balance. The class has methods for depositing and withdrawing money. We create an object account from the BankAccount class and perform deposit and withdrawal operations.

Example 2: Car Class

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer = 0

    def drive(self, miles):
        self.odometer += miles
        print(f"Drove {miles} miles. Total miles: {self.odometer}")

    def display_info(self):
        print(f"{self.year} {self.make} {self.model}, Odometer: {self.odometer} miles")

# Creating an object
car = Car("Toyota", "Corolla", 2020)
car.drive(100)  # Output: Drove 100 miles. Total miles: 100
car.display_info()  # Output: 2020 Toyota Corolla, Odometer: 100 miles

In this example, we define a Car class with attributes make, model, year, and odometer. The class has methods for driving the car and displaying its information. We create an object car from the Car class and perform driving and display operations.


This concludes our detailed tutorial on Python classes and objects. We hope you found this tutorial helpful and informative. For more tutorials and resources, visit codeswithpankaj.com. Happy coding!

Last updated