Object-Oriented Programming in Python
Welcome to our lesson on Object-Oriented Programming (OOP) in Python! As a Java developer, you’re already familiar with OOP concepts. In this lesson, we’ll explore how Python implements these concepts and highlight the key differences from Java.
Class Definition
In Python, classes are defined using the class
keyword, similar to Java. However, the syntax is more concise:
# Python
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
Compare this to Java:
// Java
public class Car {
private String make;
private String model;
public Car(String make, String model) {
this.make = make;
this.model = model;
}
}
Notice that Python doesn’t use the new
keyword when creating objects:
# Python
my_car = Car("Toyota", "Corolla")
Constructor Method
In Python, the constructor is defined using the special method __init__
. This is equivalent to Java’s constructor method:
# Python
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
The self
parameter in Python is equivalent to this
in Java.
Instance Methods
Instance methods in Python are defined within the class and always take self
as the first parameter:
# Python
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
print(f"This car is a {self.make} {self.model}")
Class Variables vs Instance Variables
Python distinguishes between class variables and instance variables:
# Python
class Car:
wheels = 4 # Class variable
def __init__(self, make, model):
self.make = make # Instance variable
self.model = model # Instance variable
Class variables are shared among all instances of the class, while instance variables are unique to each instance.
Inheritance
Inheritance syntax in Python is a bit simpler than in Java. Multiple inheritance is also supported:
# Python
class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
class Car(Vehicle):
def __init__(self, make, model, doors):
super().__init__(make, model)
self.doors = doors
class ElectricCar(Car):
def __init__(self, make, model, doors, battery_capacity):
super().__init__(make, model, doors)
self.battery_capacity = battery_capacity
Method Overriding
Method overriding in Python is straightforward:
# Python
class Vehicle:
def describe(self):
return f"This is a vehicle"
class Car(Vehicle):
def describe(self):
return f"This is a car with {self.doors} doors"
Access Modifiers
Python doesn’t have explicit access modifiers like public
, private
, or protected
. Instead, it uses naming conventions:
- No prefix: Public
- Single underscore prefix: Protected (convention only)
- Double underscore prefix: Name mangling (provides weak form of privacy)
# Python
class Car:
def __init__(self):
self.public_var = "I'm public"
self._protected_var = "I'm protected"
self.__private_var = "I'm private"
Abstract Base Classes
Python supports abstract classes through the abc
module:
# Python
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def move(self):
pass
class Car(Vehicle):
def move(self):
return "The car is driving"
This is similar to Java’s abstract classes and methods.
Conclusion
In this lesson, we’ve covered the basics of object-oriented programming in Python. We’ve seen how Python’s OOP concepts map to Java’s, and where they differ. Python’s approach to OOP is generally more flexible and less verbose than Java’s, but it still provides powerful tools for creating well-structured, object-oriented code.
In the next lesson, we’ll dive into Python’s modules and packages, exploring how to organize and reuse code effectively in larger Python projects. We’ll compare this to Java’s approach to code organization and see how Python’s import system differs from Java’s package system.