Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
OOP Terminology in Python
Object-Oriented Programming (OOP) in Python uses specific terminology to describe its concepts. Understanding these terms is essential for working with classes, objects, and inheritance in Python.
Core OOP Terms
Class
A user-defined blueprint for creating objects that defines attributes and methods. Think of it as a template that describes what data and behaviors objects will have ?
class Car:
wheels = 4 # Class variable
def __init__(self, brand):
self.brand = brand # Instance variable
def start_engine(self): # Method
return f"{self.brand} engine started"
# Creating an instance
my_car = Car("Toyota")
print(my_car.start_engine())
Toyota engine started
Object and Instance
An object is a unique instance of a class. Instantiation is the process of creating an object from a class ?
class Circle:
def __init__(self, radius):
self.radius = radius
# Instantiation - creating instances
circle1 = Circle(5)
circle2 = Circle(10)
print(f"Circle 1 radius: {circle1.radius}")
print(f"Circle 2 radius: {circle2.radius}")
Circle 1 radius: 5 Circle 2 radius: 10
Variables and Data Members
Class Variables vs Instance Variables
Class variables are shared by all instances, while instance variables belong to individual objects. Both are types of data members ?
class Student:
school_name = "Python High" # Class variable
def __init__(self, name, grade):
self.name = name # Instance variable
self.grade = grade # Instance variable
student1 = Student("Alice", "A")
student2 = Student("Bob", "B")
print(f"School: {Student.school_name}")
print(f"Student 1: {student1.name}, Grade: {student1.grade}")
print(f"Student 2: {student2.name}, Grade: {student2.grade}")
School: Python High Student 1: Alice, Grade: A Student 2: Bob, Grade: B
Advanced OOP Concepts
Inheritance
The transfer of characteristics from a parent class to child classes, allowing code reuse and creating class hierarchies ?
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal): # Dog inherits from Animal
def speak(self):
return f"{self.name} barks"
# Using inheritance
dog = Dog("Max")
print(dog.speak())
Max barks
Operator Overloading
Defining custom behavior for operators when used with objects of your class ?
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other): # Operator overloading for +
return Point(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Point({self.x}, {self.y})"
p1 = Point(1, 2)
p2 = Point(3, 4)
p3 = p1 + p2 # Uses __add__ method
print(p3)
Point(4, 6)
Summary of OOP Terms
| Term | Definition | Example |
|---|---|---|
| Class | Blueprint for creating objects | class Car: |
| Object/Instance | Specific example of a class | my_car = Car() |
| Class Variable | Shared by all instances | wheels = 4 |
| Instance Variable | Unique to each instance | self.brand = brand |
| Method | Function defined in a class | def start_engine(self): |
| Inheritance | Child class inherits from parent | class Dog(Animal): |
Conclusion
Understanding OOP terminology is crucial for Python programming. These concepts work together to create organized, reusable code through classes, objects, inheritance, and method definitions.
