Python OOP Tutorial 5: Special (Magic/Dunder) Methods

///Python OOP Tutorial 5: Special (Magic/Dunder) Methods

Python OOP Tutorial 5: Special (Magic/Dunder) Methods

FavoriteLoadingAdd to favorites

In this Python Object-Oriented Tutorial, we will be learning about special methods. These are also called magic or dunder methods. These methods allow us to emulate built-in types or implement operator overloading. These can be extremely powerful if used correctly. We will start by writing a few special methods of our own and then look at how some of them are used in the Standard Library. Let’s get started.

Python Docs:

Python OOP 1 – Classes and Instances –
Python OOP 2 – Class Variables –
Python OOP 3 – Classmethods and Staticmethods –
Python OOP 4 – Inheritance –
Python OOP 5 – Special (Magic/Dunder) Methods –
Python OOP 6 – Property Decorators –

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2020-02-15T01:49:49+00:00February 15th, 2020|Python Video Tutorials|42 Comments

42 Comments

  1. Eddie Licea February 15, 2020 at 1:49 am - Reply

    Best of the best, enough said…

  2. MCinAssembly February 15, 2020 at 1:49 am - Reply

    why am I learning python in this detail I don't even like the language

  3. Mosaab Muhammed February 15, 2020 at 1:49 am - Reply

    Thank you for sharing.

  4. ytuploads February 15, 2020 at 1:49 am - Reply

    At 5:08 you typed "print(" on TWO lines at the same time !! And then closed them both out. HOW DID YOU DO THIS?

  5. Tamir O February 15, 2020 at 1:49 am - Reply

    I love your videos man, seriously I don't get it why in college they don't explain it like you do- I just understood a lot about Python thanks to you.
    I'm dead serious I was frustrated I didn't got it right, thank you very much for sharing your knowledge !

  6. krish J February 15, 2020 at 1:49 am - Reply

    Awesome…Now I can see the more details and depth in python…

  7. Aman Saxena February 15, 2020 at 1:49 am - Reply

    You are great man!! You have helped me a lot in understanding many Python Concepts.

  8. fred tingey February 15, 2020 at 1:49 am - Reply

    These are excellent videos. Thank you!

  9. Muhammad Nur Hazrul February 15, 2020 at 1:49 am - Reply

    May i know where do you get the code to the real life examples towards the end ?

  10. Md kaif Khan February 15, 2020 at 1:49 am - Reply

    what is difference between _repr_ method and _str_ method??

  11. Kalsang Dorjee February 15, 2020 at 1:49 am - Reply

    Hehe he i got 50℅ only

  12. peraruor February 15, 2020 at 1:49 am - Reply

    Like your videos a lot. Only thing, got to concentrate a lot, which is good, you don't waist my time. Got a question. What Python editor you using? Thanks.

  13. bas pass February 15, 2020 at 1:49 am - Reply

    Hi Corey, please make video on multiple inheritance , and a video giving
    a reason that why we should not pass mutable data types as arguments

  14. bas pass February 15, 2020 at 1:49 am - Reply

    Hi Corey, please make video on multiple inheritance , and a video giving
    a reason that why we should not pass mutable data types as arguments

  15. Ibrahim Nezar Al-Mahfooz February 15, 2020 at 1:49 am - Reply

    Thank you Corey for the useful contents.

    Now I'm trying to add more than one object (emp1+emp2+emp3+…..). I tried as in the below snippet but couldn't get it to work. Perhaps due to misunderstanding of one of the concept. Appreciate your help

    def __add__(self,*args):
    ssum = self.salary + args[0].salary
    for i in range(1,len(args)-1):
    ssum = ssum + i.salary
    return ssum

    print(emp1+emp2+emp3)

  16. Nirmala Nirmi February 15, 2020 at 1:49 am - Reply

    How do you comment/uncomment multiple lines with #?

  17. Dennis Asamoah February 15, 2020 at 1:49 am - Reply

    thank you. God bless you

  18. Subscribe to C4ETech February 15, 2020 at 1:49 am - Reply

    Thala nee vera level Thala ! <3

  19. linda ph February 15, 2020 at 1:49 am - Reply

    Thank you so much, Corey!

  20. Uli Un February 15, 2020 at 1:49 am - Reply

    Dunder is a really stupid word. Whoever coined this word is a bitch.

    Superb tutorial nonetheless.

  21. Varghese Jose February 15, 2020 at 1:49 am - Reply

    Absolutely recommended video. Crisp and Clear

  22. Siva Sai February 15, 2020 at 1:49 am - Reply

    Great Videos from you, You are helping many python newbies. please continue further with new python videos.

  23. Linda Phan February 15, 2020 at 1:49 am - Reply

    Amazing 👌 teacher!

  24. Manuel Alejandro Salazar Gómez February 15, 2020 at 1:49 am - Reply

    Hi Corey. Iwould like to know what would you do if you need to add multiple employees salaries? e.g. emp_1 + emp_2 + emp_3. Thank you, nice videos.

    Edit: Hope somebody can answer this question

  25. Sharon Jin February 15, 2020 at 1:49 am - Reply

    What's the IDE in this video?

  26. Ashley Cichocki February 15, 2020 at 1:49 am - Reply

    Another excellent tutorial. I didn't really get magic methods until I watched this. Clear, thorough and concise. Thank you!

  27. Yashu Yashwant February 15, 2020 at 1:49 am - Reply

    can you please provide the frame work that you have shown at the end of the video

  28. Daniel Weikert February 15, 2020 at 1:49 am - Reply

    Hi Corey, thanks so much for your great content. Great videos? Could you make a tutorial on the other dunder functions? getitem, call, iter,….. That would be awesome. Thanks and best!

  29. türker göktaş February 15, 2020 at 1:49 am - Reply

    Guys i wanted to ask something about the _add_ method it seems like it can take only 2 variables like emp_1 and emp_2.What should i do if i want to add lets say emp_3 also.I tried it gives error

  30. Bruno Almeida February 15, 2020 at 1:49 am - Reply

    Thanks, again!

  31. Mona Gulapa February 15, 2020 at 1:49 am - Reply

    When I was self studying C# because my job needed it, I hardly grasped the concept of OOP because coding it in C# to see how it actually works is complicated. With your explanation, I saw the clear and complete picture of OOP. Maraming Salamat ! (Thank you so much ! 🙂)

  32. kamlesh dabi February 15, 2020 at 1:49 am - Reply

    Hey, first of all thanks for the videos. i am new in programming and started just while ago.
    I am going through all your videos and the examples, you are using, are up to the topic. So, can you provide me some link or documentation, where i can get some other, same kind of, examples (on functions,classes, inheritance etc.) for practice ??

  33. Chris Keo February 15, 2020 at 1:49 am - Reply

    Dunder mifflin

  34. nikhil nanal February 15, 2020 at 1:49 am - Reply

    How does the print(emp1 + emp2) know that when + is used in the call the _add_ method in the class
    has to be called?

  35. amit naik February 15, 2020 at 1:49 am - Reply

    Thank You for helping fellow like me 😀

  36. SAJIDUR RAHMAN February 15, 2020 at 1:49 am - Reply

    One of the best python tutorials I’ve ever seen

  37. Tesla February 15, 2020 at 1:49 am - Reply

    please tell me what i should override to do to access the instance variables like this:

    TCP_Layer = scapy_packet[scapy.TCP]

  38. mogpone February 15, 2020 at 1:49 am - Reply

    how about this for the manager 😀

    def __add__(self, emp):
    self += emp
    return self

    def __sub__(self, emp):
    self -= emp
    return self

    def __iadd__(self, emp):
    self.add_emp(emp)
    return self

    def __isub__(self, emp):
    self.remove_emp(emp)
    return self

  39. Roshan Yerragudi February 15, 2020 at 1:49 am - Reply

    Hi Corey, I'm following your videos to learn python.your videos are just awesome….
    and my doubt is: can we use this method to print objects instead of _str_ method
    print_object(self):
    return '{} – {}'.format(self.fullname(), self.email)

    and this method also gives the correct output
    so what is the difference between those

  40. Farhanking7864 February 15, 2020 at 1:49 am - Reply

    A video on overloading would be awesome

  41. AllomerusMusic February 15, 2020 at 1:49 am - Reply

    Excellent explanation Corey. Thanks for sharing this!

  42. Ms.RicosMathTube February 15, 2020 at 1:49 am - Reply

    When I added the _str_ code it returned the following:
    <bound method Employe.fullname of Employee(…..) – email address

    anyone have a clue what happened?

Leave A Comment

*