0% found this document useful (0 votes)
7 views4 pages

Python OOP Magic Methods & Relationships

The document explains key Object-Oriented Programming (OOP) concepts in Python, including Magic/Dunder Methods, Composition, Association, and Aggregation. It provides definitions, examples, and short explanations for each concept, illustrating how classes can interact and depend on one another. The document also includes a roadmap outlining these OOP relationship concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Python OOP Magic Methods & Relationships

The document explains key Object-Oriented Programming (OOP) concepts in Python, including Magic/Dunder Methods, Composition, Association, and Aggregation. It provides definitions, examples, and short explanations for each concept, illustrating how classes can interact and depend on one another. The document also includes a roadmap outlining these OOP relationship concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Magic/Dunder Methods
Definition:
Special methods in Python surrounded by double underscores ( __method__).

They allow you to define how objects behave with built-in operations (printing, addition, length, etc.).

Example:

class Book:
def __init__(self, title, author, pages):
[Link] = title
[Link] = author
[Link] = pages

def __str__(self): # When you use print(object)


return f"'{[Link]}' by {[Link]}"

def __len__(self): # When you use len(object)


return [Link]

b = Book("Python Basics", "Sriram", 250)

print(b) # Calls __str__


print(len(b)) # Calls __len__

Output:

'Python Basics' by Sriram


250

Short Explanation:

 __init__ → initializes attributes.


 __str__ → custom string representation.
 __len__ → defines length of object.

2. Composition
Definition:
One class contains objects of another class as part of its attributes. (“Has-a” relationship)

Example:

class Engine:
def start(self):
return "Engine started"

class Car:
def __init__(self, brand):
[Link] = brand
[Link] = Engine() # Composition
def start(self):
return f"{[Link]} car -> {[Link]()}"

c = Car("Toyota")
print([Link]())

Output:

Toyota car -> Engine started

Short Explanation:
Car has an Engine object. Without Engine, Car is incomplete.

3. Association
Definition:
A general relationship where two classes are connected, but neither owns the other. They can exist
independently.

Example:

class Teacher:
def __init__(self, name):
[Link] = name

def teach(self):
return f"{[Link]} is teaching."

class Student:
def __init__(self, name):
[Link] = name

def learn(self):
return f"{[Link]} is learning."

t = Teacher("Mr. Rao")
s = Student("Anil")

print([Link]())
print([Link]())

Output:

Mr. Rao is teaching.


Anil is learning.

Short Explanation:
Teacher and Student know each other but are not dependent on each other to exist.
4. Aggregation
Definition:
A “Has-a” relationship where the contained object can exist without the container.

Example:

class Department:
def __init__(self, name):
[Link] = name

class Employee:
def __init__(self, name, department):
[Link] = name
[Link] = department # Aggregation

def info(self):
return f"{[Link]} works in {[Link]}."

d = Department("HR")
e = Employee("Kavya", d)

print([Link]())

# Department still exists even if Employee is deleted


del e
print([Link])

Output:

Kavya works in HR.


HR

Short Explanation:

 Employee has a Department reference.


 Department can exist even without Employee.

Road Map for This Topic


OOP Relationship Concepts in Python

OOP Concepts
├── Magic / Dunder Methods
│ ├── __init__ → Initialization
│ ├── __str__ → String representation
│ ├── __len__ → Object length
│ ├── __add__, __eq__, etc.

├── Composition
│ └── Class A has Class B object (strong dependency)

├── Association
│ └── Two classes interact but are independent

├── Aggregation
│ └── Has-a relationship (loose dependency)

You might also like