Python Classes and Objects - Complete Guide
Classes let you create your own data types in Python. They're the foundation of object-oriented programming (OOP). Let's learn how to use them!
What is a Class?
A class is a blueprint for creating objects. Think of it like a cookie cutter - the class is the cutter, objects are the cookies!
class Dog:
pass
# Create an object (instance)
my_dog = Dog()Creating a Simple Class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"Hi, I'm {self.name} and I'm {self.age} years old"
# Create objects
person1 = Person("John", 25)
person2 = Person("Jane", 30)
print(person1.introduce()) # Hi, I'm John and I'm 25 years old
print(person2.introduce()) # Hi, I'm Jane and I'm 30 years oldThe init Method
__init__ is called when an object is created. It initializes the object:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
print("Person created!")Instance Variables
Variables that belong to each object:
class Person:
def __init__(self, name):
self.name = name # Instance variable
self.age = 0 # Instance variable
person = Person("John")
person.age = 25 # Set instance variableMethods
Functions that belong to a class:
class Calculator:
def __init__(self):
self.result = 0
def add(self, value):
self.result += value
return self.result
def subtract(self, value):
self.result -= value
return self.result
calc = Calculator()
print(calc.add(10)) # 10
print(calc.subtract(3)) # 7Class Variables
Variables shared by all instances:
class Dog:
species = "Canis familiaris" # Class variable
def __init__(self, name):
self.name = name # Instance variable
dog1 = Dog("Buddy")
dog2 = Dog("Max")
print(dog1.species) # "Canis familiaris"
print(dog2.species) # "Canis familiaris"
print(Dog.species) # "Canis familiaris"Inheritance
Create new classes based on existing ones:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Woof!
print(cat.speak()) # Meow!Method Overriding
Override parent class methods:
class Animal:
def make_sound(self):
return "Some sound"
class Dog(Animal):
def make_sound(self):
return "Woof!" # Overrides parent methodSuper() Function
Call parent class methods:
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call parent __init__
self.breed = breedEncapsulation
Control access to variables and methods:
class BankAccount:
def __init__(self, balance):
self._balance = balance # Protected (convention)
self.__pin = 1234 # Private (name mangling)
def get_balance(self):
return self._balance
def deposit(self, amount):
if amount > 0:
self._balance += amountProperties
Use properties for getters and setters:
class Person:
def __init__(self, age):
self._age = age
@property
def age(self):
return self._age
@age.setter
def age(self, value):
if value < 0:
raise ValueError("Age cannot be negative")
self._age = value
person = Person(25)
print(person.age) # 25
person.age = 30 # Uses setterClass Methods and Static Methods
Class Methods
class Person:
count = 0
def __init__(self, name):
self.name = name
Person.count += 1
@classmethod
def get_count(cls):
return cls.count
print(Person.get_count()) # 0
person1 = Person("John")
print(Person.get_count()) # 1Static Methods
class Math:
@staticmethod
def add(a, b):
return a + b
print(Math.add(5, 3)) # 8Special Methods (Magic Methods)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"Person({self.name}, {self.age})"
def __repr__(self):
return f"Person(name='{self.name}', age={self.age})"
def __eq__(self, other):
return self.name == other.name and self.age == other.age
person = Person("John", 25)
print(person) # Uses __str__
print(repr(person)) # Uses __repr__Visual Explanation: Class Structure
Here's how classes work:
Class Definition:
┌─────────────────────────────┐
│ class Person: │
│ def __init__(self, name)│
│ self.name = name │
│ │
│ def greet(self): │
│ return "Hello" │
└─────────────────────────────┘
│
│ Creates instances
│
┌────┴────┐
│ │
▼ ▼
┌────────┐ ┌────────┐
│Person1 │ │Person2 │
│name │ │name │
│greet() │ │greet() │
└────────┘ └────────┘Frequently Asked Questions (FAQ)
Q1: What's the difference between class and object?
A:
- Class - Blueprint/template
- Object - Instance created from class
class Person: # Class
pass
john = Person() # Object (instance)Q2: What is self?
A: self refers to the current instance. It's like "this" in other languages. You must include it as the first parameter in methods.
Q3: What's init?
A: __init__ is a special method called when an object is created. It initializes the object:
def __init__(self, name):
self.name = nameQ4: What's the difference between class and instance variables?
A:
- Class variable - Shared by all instances
- Instance variable - Unique to each instance
Q5: What is inheritance?
A: Creating a new class based on an existing class. The new class inherits all methods and variables from the parent:
class Child(Parent):
passQ6: What's method overriding?
A: When a child class defines a method with the same name as the parent, it overrides (replaces) the parent's method.
Q7: What's super()?
A: super() lets you call parent class methods:
super().__init__(name) # Call parent __init__Q8: What are private variables?
A: Variables starting with __ are private (name mangling). Variables starting with _ are protected (convention):
self.__private = 10 # Private
self._protected = 20 # ProtectedQ9: What's the difference between @classmethod and @staticmethod?
A:
- @classmethod - Receives class as first parameter
- @staticmethod - Doesn't receive class or instance
Q10: What are magic methods?
A: Special methods with double underscores like __init__, __str__, __eq__. They define how objects behave:
def __str__(self): # Called by print()
return "String representation"Next Steps
Now that you understand classes, here's what to learn next:
- Next: Learn about Python Modules - Organize your classes
- Explore Python File Handling - Work with files
- Understand Python Exception Handling - Handle errors
- Master Python Decorators - Advanced Python features
Classes are powerful in Python. Practice creating classes and objects to get comfortable!