Learn to Program 10 : Inheritance Magic Methods

[ad_1]
Code & Transcript :

Interactive Tutorial with Quiz Here :

Best Python Book :

Support me on Patreon :

In this part of the tutorial I’ll cover Inheritance, Operator Overloading, Polymorphism and Magic Methods, which are super awesome! If you missed previous parts of this tutorial watch those videos first.

Thank you to Patreon supports like the following for helping me make this video

@kyleaisho
@thetwistedhat
vjFaLk


Posted

in

by

Tags:

Comments

38 responses to “Learn to Program 10 : Inheritance Magic Methods”

  1. Romy Kim Avatar

    Hi Derek. Quick question. What do you call this double underline at the front only ? It is not dunder/magic or private variable. It is at 7:35 ish as in, self.__nurseYoung = nurseYoung
    Thanks.

  2. Tranculent Avatar

    You really are going through the things pretty fast and there is lack of explanation. I liked your previous videos but this one is pretty hard to understand.

  3. namtulla najmidin Avatar

    i think the magic mathod is same as the some of the static method in java. for example _str_ == toString()

  4. SiGnAtUrEaDD Avatar

    Hi Derek! Nice video, but I was just wondering what is the purpose of this line of code in your time class example:

    def __add__(self, other_time):
    new_time = Time() (*this line*)

    # Add the seconds and correct if sum > 60
    if (self.second + other_time.second) >= 60:
    self.minute += 1
    new_time.second = (self.second + other_time.second) – 60

    else:
    new_time.second = self.second + other_time.second

  5. Rajat Malviya Avatar

    Why do u write self.__birthtype instead of self.birthtype???
    And also what is __name__??

  6. Arya Bhatta Avatar

    IS ALL VIDEO HERE FOR PYTHON 2 OR 3 ?

  7. Parafian12 Avatar

    Sorry I'm getting confused a looot. One of my question.
    if we make _add_ function and pass there a other_time, it already knows that it is something we want add too, and immediately creats a full object? If so, couldn't we pass _add_(self, other_time, new_time) instead of creating it inside of the function?
    Thanks Derek for videos!!!!

  8. Fallacy Avatar

    Hey Derek, I really appreciate you doing this, great work and great examples not too hard but also not too easy. Thank you!
    Alternative solution for the time addition(The rest ramains the same):

    def _add_(self, other_time):
    new_time = Time()

    new_time.second = self.second + other_time.second

    if new_time.second >= 60:

    new_time.second = new_time.second % 60

    self.minute += 1

    new_time.minute = self.minute + other_time.minute

    if new_time.minute >= 60:

    new_time.minute = new_time.minute % 60

    self.hour += 1

    new_time.hour = self.hour + other_time.hour

    if new_time.hour >= 24:

    new_time.hour = new_time.hour % 24

    return new_time

  9. ALLAINE GAMING Avatar

    Oh uh Mr Derek Banas haven't you try the Treehouse code i seen it at the Youtube commercial!!

  10. manish adwani Avatar

    in else why did you white self.sec/self.minute – othertime.sec/minute

  11. manish adwani Avatar

    if you want a full working clock just make while loop with condition like second is greater than 60, then increment minute by 1 and subtract 60 from seconds did you get it..!

    how's that Derek!

  12. Filippo Gentile Avatar

    I think there is a logical mistake when you overwrite the _sum_ operator in the class Time. Indeed, when you add two Time objects, you don't want one of the objects to be changed. In the details, time1 + time2 should not modify the values of either time1 and time2. In your implementation: self.minute +=1 (you do the same also for the hours..) you changes the value of the first operand of the addition. I know that you are not seeking for the best implementation, but definitely this implementation does not respect the expected behavior of the _add_ operator!Thanks for sharing the videos!

  13. s3icc0 Avatar

    I've seen already multiple tutorials to OOP and I read huge amount of articles and forums, but I still don't fully understand the meaning of not using underscore using one or using two. Can you explain please like to a monkey? 🙂

  14. Kritika Bansal Avatar

    The thing you did to print the name of the class i.e type(self).__name__ . It doesn't work in python 3. Instead of the name of the class it's printing the word 'instance'.
    I tried with self.__class__.__name__ and that works!

  15. Younes PhD Henni Avatar

    Hi Derek,
    Why you did not create setters and getters for object Time ?

  16. Amol Srivastav Avatar

    @Derek…Can you create a course on data structures and algorithms in python

  17. Omuro makori Avatar

    i love how you teach as well as you coding style…it makes everything so easy

  18. mubarak l Avatar

    Hello Derek, you are too fast man. It is very difficult for beginners to catch your words. Please go slow and explain by giving general examples. Please very confusion dear.

  19. Zohaib Mahmood Avatar

    Is it possible to inherit private variables (in the init method) from a child class? Or are the variables only accessible in the parent class only?

  20. MouhamedSaid RAOUDH Avatar

    Traceback (most recent call last):
    File "/Users/mouhamedsaidrawdh/PycharmProjects/firstPython/inheritencePolimprphismMagicMethod.py", line 75, in <module>
    main()
    File "/Users/mouhamedsaidrawdh/PycharmProjects/firstPython/inheritencePolimprphismMagicMethod.py", line 71, in main
    animal1 = Animal("born alive")
    File "/Users/mouhamedsaidrawdh/PycharmProjects/firstPython/inheritencePolimprphismMagicMethod.py", line 3, in _init_
    self.blooded = blooded
    AttributeError: can't set attribute

    all the code is right.
    i don't know what should i do , need help pls
    thank you

  21. Oco ro Avatar

    what is the point of all the getters and setters and why not just have them built in the language by default? it just looks like a lot of clutter to me and the syntax looks pretty wack too(like why isn't it just @setter? the property is just @property). godbless

  22. Andy Herz Avatar

    Great vid.
    But still I am confused with getters, setters and inheritance. Is there any books with examples for beginners?

  23. Ben Elder Avatar

    I'm finally starting to mess with inheritance, I've got the getter/setters working, but was wondering why you call the class name in the "__init__" in the subclass, rather than the "super()"?
    I tried using the super() command, but got an error.

    In your example it would be "super().__init__(self, blah, blah, blah)", but you have "Animal.__init__(self, blah, blah, blah)"

  24. Chris Kavanagh Avatar

    Why not use 'Super' on your Constructor Methods, Derek? If you're going to use them in the other methods, makes sense to use them with your _init_ too.

  25. KonchuKangaroo Avatar

    7:59 "Nurse Jeung" Best Asian doctor. Thank you Derek! You helped me a lot in my programming challenge!

  26. DurdenTyler3 Avatar

    Hi Derek,
    Question: I read somewhere that single underscore '_' before a name means it's private and double underscore '__' before a name means python will "mangle" it with the class name in order to prevent namespace clashing (e.g. '__sum' in class A is being replaced with '_A__sum'). But we are using double underscore for private. What am I missing? ~source: https://docs.python.org/3.4/tutorial/classes.html#tut-private

    Thanks!!

  27. LiorDolinski Avatar

    A small question:
    I still didn't get why do we use getters and setter.. (@property and@.setter)
    what's the purpose of them? specifically in this code (the animals one).
    It seems like they don't serve any purpose.. just assigning the same values to the attributes.

    Could you elaborate a bit more on those? and perhaps also about the @staticmethod vs @classmethod ?

    Thanks Derek you are truly awesome.

  28. DJ Avatar

    How would one go about overloading _mul_ (or any operator) where the object may be passed in as the 1st or 2nd parameter? I was able to make "timeObject * 5" work, but "5 * timeObject does not. Thanks Derek, these videos are great!

  29. michalxi1410 Avatar

    Hi, why you use Animal.__init__(….) and then super().__str__()? Why no start all with Animal. or start all with super().? What is the difference?

  30. HE WANG Avatar

    Hi Derek,

    Thanks for you tutorials, they are awsome!

    I just wondering if you have any tutorial about the decorator? Cheers

  31. Cuong Bang Avatar

    Just my 2 cents, I don't think it's a good idea to change values of properties of "self" in the magic function _add_ of Time class.

  32. arielkan98 Avatar

    So essentialy magic methods are used to override built-in methods in Python, is that correct?

  33. Feng-I Lin Avatar

    Thank you for sharing. I like your video.
    a confusion about : + or –
    as I realised, +

    line 19
    new_time.second= self.second + other_time.second

    line 26:
    new_time.minute = self.minute + other_time.minute

  34. Сергей Мосягин Avatar

    Its first complicated series, but still interesting. I am keep watching, thank you!

  35. Z Dax Avatar

    I spent 20+ minutes trying to fix my error: 'blooded' is not defined , the error was in the @blooded.setter section, did not change the copied code. haha

  36. YONG JIE Avatar

    sometimes we need to put double underscore before fields (like self.__nurseYoung) sometimes we do not (self.nurseYoung. What is the rule here?

  37. FireClocks Avatar

    Hi Derek! Nice Video! I have a question (which is not really related to the video but I would be cool if you could answer). Which programming language do graphic calculators use? (like Casio). Can I make some cool programs there? For like, when I'm bored at school xD!

Leave a Reply

Your email address will not be published. Required fields are marked *