-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic.py
More file actions
38 lines (30 loc) · 1.25 KB
/
magic.py
File metadata and controls
38 lines (30 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Magic methods are also called Dunder methods (double underscore)
# They are automatically called by Python's built-in operations such as +, -, >, < and so on
# They allow to define or customize the behavior of operations
class Book:
def __init__(self, title, author, num_pages):
self.title = title
self.author = author
self.num_pages = num_pages
def __str__(self): # The __str__ method in Python is called when a string representation of an object is needed.
return f"'{self.title}' by {self.author}"
def __eq__(self, other):
return self.author == other.author
def __gt__(self, other):
return self.num_pages > other.num_pages
def __getitem__(self, key):
if key.lower() == "author":
return self.author
elif key.lower() == "title":
return self.title
elif key.lower() == "num_pages":
return self.num_pages
else:
print(f"Key '{key}' not found in {self.title}")
book1 = Book("The Da Vinci Code", "Dan Brown", 435)
book2 = Book("A Thousand Splendid Suns", "Khaled Hosseini", 354)
book3 = Book("Midnight's Children", "Salman Rushdie", 250)
print(book1)
print(book2 == book1)
print(book1 > book2)
print(book1["author"])