# Magic methods = Dunder methods (double underscore) ___init__, __str__, __eq___ # They are automatically called by many of Python's built-in operations. # They allow developers to define or customize the behavior of objects class Book: def __init__(self, title, author, num_pages): self.title = title self.author = author self.num_pages = num_pages def __str__(self): return f"'{self.title}' by {self.author}" def __eq__(self, other): return self.title == other.title and self.author == other.author def __lt__(self, other): return self.num_pages < other.num_pages def __gt__(self, other): return self.num_pages > other.num_pages def __add__(self, other): return f"{self.num_pages + other.num_pages} pages" def __contains__(self, keyword): return keyword in self.title or keyword in self.author def __getitem__(self, key): if key == "title": return self.title elif key == "author": return self.author elif key == "num_pages": return self.num_pages else: return f"Key '{key}' was not found" book1 = Book("The Hobbit", "J.R.R. Tolkien", 310) book2 = Book("Harry Potter and The Philosopher's Stone", "J.K. Rowling", 223) book3 = Book("The Lion, the Witch and the Wardrobe", "C.S. Lewis", 172) print(book1) # Calls ___str___ print(book1 == book3) # Calls ___eq___ print(book1 < book2) # Calls ____lt___ print(book2 > book3) # Calls ___gt___ print(book1 + book2) # Calls ___add___ print("Lion" in book3) # Calls ___contains___ print(book3['title']) # Calls ___getitem___
@HojiakbarHabibullayev-jj6db3 ай бұрын
Hi bro thank you for lessons I like your you tube channel
@shirinMN11 күн бұрын
you are so amazing i like the way you teach and your voice too💯
@jankiwen31124 ай бұрын
Thank you so much! I've been seeing those weird double underscores in so many other tutorials (completely unrelated to dunder methods) but only now have I found a video explaining them in such an approachable way. Python is my favorite programming language, I'm glad to learn it more thoroughly.
@agyekumderrick94494 ай бұрын
Bro to the world👊🏼, thanks for the updates all the time
@Hussain.1154 ай бұрын
Thankgod after spending 2 days now I got what actually these methods are 😊 after watching many videos from different channels its okay to check other sources.
@italianoconmohamed22333 ай бұрын
same here 😅
@oliya_b25 күн бұрын
Thank you for this clear explanation! You're such a good teacher! Thank you a lot😊
@vigneshs684217 күн бұрын
Basically we are using method overriding with built in methods. For example. > op will take either str or int , but we are overriding the method to compare object attributes
@uzairzarry86914 ай бұрын
Great explanation❤
@matchalover084 ай бұрын
Just started a new semester on algorithms, this posted at the right time for my revision! Thx bro!
@zedzpan4 ай бұрын
Love how you made this so simple to understand. Dunder methods were always weird to me!
@KnownUnknown52324 ай бұрын
Bro you deserve way more likes
@ashwalker14 ай бұрын
Thank you Bro Can't wait for video on gaming channel
@nazihakh35174 ай бұрын
you are awesome! thanks bro!
@nasiriqbal54754 ай бұрын
Brooooo, what did you study in college? only Computer Science or other extensive subjects too? Btw i learnt python from you and am currently learning html and css, thank you bro, bootcamps hate you fr.
@MarianoBustos-i1f4 ай бұрын
Thanks Bro
@patrickjohnsanchez94274 ай бұрын
Bro Next video series please node js thanks bro
@JAYY16314 ай бұрын
he's awesome bro
@MFaiqVaince4 ай бұрын
Can you plz update your c# playlist with more stuff after this, it will be very handy
@cbbcbb68033 ай бұрын
I really like your tutorials. But, I hate having to contort my fingers in order to key in special chaeacters!
@kapibara24404 ай бұрын
Great video, thank you🎉🎉🎉
@PriyanshModanwal4 ай бұрын
when will u drop the course ?
@FromTheWombTotheGrave4 ай бұрын
Can u make vid about Python AI bot creations/LLMs And automations with AI Creating videos with AI in Python Or automated instagram theme page run by AI
@botpad49214 ай бұрын
bro, i have learned web dev from ur playlist, but now i need a course for certificates, can u tell me what to do for the certificates? btw ur videos are too good fr😎😎
@vitavita55094 ай бұрын
bro thx
@Build33_Offical4 ай бұрын
L e g e n d
@ማዕዶት_tube4 ай бұрын
Pls bro code❤❤❤😊
@rawa977754 ай бұрын
Bro please a tutorial about discord python. Please bro code
@robert-qn8hy4 ай бұрын
can you please make a video on python generators and asynchorous programming please