Python OOP Concepts
Python OOP Concepts
This tutor guide is designed to teach Python Object-Oriented Programming (OOP) concepts using
w3schools.com as the primary source. It covers the fundamental principles of OOP in Python, including
classes, objects, inheritance, polymorphism, encapsulation, and abstraction. Each section includes key
characteristics, example usage, and references to w3schools.com for detailed tutorials and interactive
examples.
Object-Oriented Programming (OOP) is a programming paradigm that organizes code using objects and
classes, making it easier to manage and reuse. Python, as an object-oriented language, supports OOP to
create structured, maintainable, and scalable applications.
Classes are user-defined data types that serve as blueprints for creating objects. Objects are instances of
classes that hold specific data and can perform actions defined by the class.
a. Creating a Class
Example:class MyClass:
x=5
Reference: Python Classes
b. Creating an Object
Example:p1 = MyClass()
print(p1.x) # Output: 5
Example:class Person:
self.name = name
self.age = age
p1 = Person("John", 36)
Example:class Person:
self.name = name
self.age = age
def __str__(self):
return f"{self.name}({self.age})"
p1 = Person("John", 36)
e. Object Methods
Methods are functions defined within a class that operate on its objects.
Example:class Person:
self.name = name
self.age = age
def myfunc(self):
p1 = Person("John", 36)
The self parameter refers to the current instance of the class, used to access its attributes and methods.
Example:class Person:
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
p1 = Person("John", 36)
p1.myfunc() # Output: Hello my name is John
Example:p1.age = 40
Example:del p1.age
i. Deleting Objects
Use the del keyword to delete an object.
Example:del p1
3. Inheritance
Inheritance allows a class (child) to inherit methods and properties from another class (parent),
promoting code reuse.
a. Parent Class
Example:class Person:
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
x = Person("John", "Doe")
b. Child Class
A child class inherits from a parent class using the parent class name in parentheses.
Example:class Student(Person):
pass
x = Student("Mike", "Olsen")
The child class can define its own __init__() function, calling the parent’s __init__() explicitly.
Example:class Student(Person):
x = Student("Mike", "Olsen")
The super() function calls the parent class’s methods, including __init__().
Example:class Student(Person):
super().__init__(fname, lname)
self.graduationyear = year
e. Adding Properties
Example:class Student(Person):
super().__init__(fname, lname)
self.graduationyear = year
f. Adding Methods
Example:class Student(Person):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
Polymorphism allows methods to perform different actions based on the object calling them, enhancing
flexibility.
a. Function Polymorphism
Built-in functions like len() work differently for various data types.
b. Class Polymorphism
Different classes can have methods with the same name but different implementations.
Example:class Car:
def move(self):
class Boat:
def move(self):
car = Car()
boat = Boat()
x.move()
# Output:
# I am moving on 4 wheels
# I am moving on water
c. Inheritance Polymorphism
Child classes can override parent class methods to provide specific implementations.
Example:class Vehicle:
def move(self):
print("I am moving")
class Car(Vehicle):
def move(self):
car = Car()
5. Encapsulation
Encapsulation involves bundling data and methods within a class and restricting access to some
components to protect data integrity.
Python achieves encapsulation using naming conventions: a single underscore (_) for protected variables
and double underscores (__) for private variables (name mangling).
Example:class MyClass:
def __init__(self):
def get_private_var(self):
return self.__private_var
obj = MyClass()
Note: Python’s private variables are not strictly private but use name mangling to discourage direct
access.
6. Abstraction
Abstraction hides complex implementation details, exposing only the necessary features through a
simplified interface.
In Python, abstraction is achieved through class design, where methods provide a user-friendly interface
without revealing internal workings.
Example:class ComplexNumber:
self.real = real
self.imag = imag
def __str__(self):
c1 = ComplexNumber(1, 2)
c2 = ComplexNumber(3, 4)
c3 = c1.add(c2)
print(c3) # Output: 4 + 6i
The user interacts with the add method without needing to understand the internal logic of complex
number addition.
7. Additional Resources
Interactive Examples: w3schools.com offers “Try it Yourself” examples for each concept, accessible via
the links provided in each section.
Related Topics: Explore related Python topics like modules and iterators at Python Tutorial.
Step 1: Start with the introduction to understand the role of OOP in Python.
Step 4: Understand polymorphism and how methods can behave differently for different objects.
Step 5: Explore encapsulation and abstraction to learn how to manage data access and complexity.
Step 6: Use the “Try it Yourself” examples on w3schools.com for hands-on practice.
Concept
Description
Reference
Classes
Objects
Instances of classes
Python Classes
Inheritance
Python Inheritance
Polymorphism
Python Polymorphism
Encapsulation
Python Classes
Abstraction