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.
- Introduction to Object-Oriented Programming
- Defining a Class
- Creating Objects
- Class Attributes and Methods
- The
__init__Method - Instance Attributes
- Instance Methods
- Practical Examples
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.
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
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.
class ClassName:
# class attributes and methods# 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.
Objects are instances of a class. You can create multiple objects from a single class.
# 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 30In 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.
Class attributes are variables that belong to the class. Methods are functions defined inside the class that describe the behaviors of the objects.
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 sapiensIn 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.
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.
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.
Instance attributes are variables that are specific to an object. They are defined within the __init__ method and prefixed with self.
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 30In 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.
Instance methods are functions defined inside a class that operate on instances of the class. They are defined with self as their first parameter.
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.
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 fundsIn 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.
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 milesIn 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!