Raymond Hettinger - Super considered super! - PyCon 2015

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ธ.ค. 2024

ความคิดเห็น • 99

  • @bbckyotoku
    @bbckyotoku 4 ปีที่แล้ว +31

    "Because his talk is on concurency, so I presume he is giving his talk and listening in this room at the same time" 20:50

  • @CodeOptimism
    @CodeOptimism 4 ปีที่แล้ว +5

    Browsing Raymond Hettinger talks since they're awesome. Clicked this one thinking, "eh it's old and just about super() probably won't be very interesting". Hah! Amazing talk.

  • @VCR47527
    @VCR47527 ปีที่แล้ว +1

    Few questions about DI in this style.
    1. How do you handle injecting a class that itself needs dependencies injected? Does the entire app become one class eventually?
    2. How do you track down everything that can be injected and what method signatures are expected for each?
    3. What happens if an interface is implemented wrongly and has a wrong return type?
    4. What happens if two different dependencies that are both required happen to have the same method names?
    5. Looks like you choose concrete implementations at the time of class declaration instead of within a configuration file (which governs class instantiation later on) like the DI libraries that I'm used to. What if I don't know some of the concrete dependencies at the time of class declaration of the base class and I only know that it will follow a certain interface?
    DI is usually a tool for class composition. In other words it deals with "has-a" relationships rather than "is-a" relationships. So it's really strange to see a language feature that facilitates class-based inheritance being suggested as a tool for DI. The result seems to be similar to a mixin pattern for sharing functionality (with all its criticisms) with extra attention to the MRO navigate the confusing nature of this language feature.

    • @andreienache6290
      @andreienache6290 11 หลายเดือนก่อน

      1. The same, just inject previously into the class you need. No, the entire app does not become 1 class.
      2. Not sure what you mean by 'track down'? Don't overcomplicate things, ptyhon design is different. Don't bring here fancy terms like 'method signatures', they don't make sense in this context.
      3. Change your code.
      4. Good point. The method resolution order will resolve to the first one encountered if you use super with no arguments. Super can be used with arguments that direct its resolution. So, without arguments, it resolves with the c3 algorithm. With arguments you can call 1 or both in the order you need ( In that specific case you do introduce a little redundancy, but you have no choice).
      5. Again, python is more abstract. An interface is just a contract or a declaration of behavior. You could just
      class Interface:
      def act(self): pass
      class Imp(Interface):
      pass
      You could use the ABC (Abstract Base Class) module to decorate and express and force the interpreter to put some restrictions, like, not implementing Interface.act method. Don't let them confuse you with fancy words 'implements' 'extends' and 'injection', behind the scenes there are just objects and pointers. In Python, I don't care if put my dependency inside the parenthesis of a class declaration if I'm given the dependency at the instance creation level or if I fucking give a framework a yaml file that declares my dependencies and makes the symbols available at the necessary scope. It's a matter of understanding the best approach for you. Again, in this case, is trivial, discussing the 'is-a' or 'has-a' thing is a waste of time and is inherited by old paradigms. The difference is just this:
      class Dependant(Dependency): # what you call 'is-a'
      pass
      or
      class Dependant: # what you call 'has-a'
      def __init__(self, dependency):
      self.dependency = dependency
      or, if you want a framework:
      dependencies.yaml
      Dependant
      Dependency
      -- Then in your code:
      Dependant().dependency.method_on_dependency()
      Do you see how dumb it is to waste time discussing this? It's nice as a learning exercise but at the end of the day, why the hell i would go and create a file and install a framework if I can just write class A(B): pass? What is the advantage of expressing this particular case in a file instead of in the code file itself? If you find dependencies later, just go and add a new dependency to your class Instantiation (Yes, classes in Python are first-class citizens).
      You are too biased. If your dependency is against an interface, and you know it, then add the interface. If you don't know it, it is not a problem with the language. That's it. When you call the method it will just do nothing, or again, you can decorate to enforce classic oo behavior like raising a NotImplemented error and stuff like that, or even raise a not implemented error. Python is very abstract and that is why it gives you so much flexibility.
      These ideas become clear to you when you become older. You need to work in too many different areas and technologies to understand all is reduced to a matter of convenience and automation. Some principles should guide you, but these are not dependent on the technologies. Behind the scenes, all is reduced to objects and pointers. How do you think a class Inheritance is resolved inside the compiler?
      So, for example, they say "Favor composition over inheritance". Ok. That may be good for Java | C# | C++ but, what if I have a language that has a method resolution and leaves me to declare my dependencies at class Instantiation level, I mean here: class A(B, C). Then what is the difference between receiving the dependency from a config file, or 'injected' at the instance instantiation level? I can test and refactor at any time. Convenience, Man.
      Hope you undersand.

  • @김-e7v6m
    @김-e7v6m 2 ปีที่แล้ว

    I've never seen such a humorous but useful talks like this !

  • @DanVanderkam
    @DanVanderkam 9 ปีที่แล้ว +9

    Great talk! Raymond suggests using multiple inheritance and super() instead of mocks in unit tests. Is this how mocking libraries in Python are typically implemented?

  • @Rahuls-Insights
    @Rahuls-Insights 9 ปีที่แล้ว +2

    This is great!!! Makes our life simpler!!! Thanks Raymond!!!

  • @swadhikarc7858
    @swadhikarc7858 5 ปีที่แล้ว +1

    Absolutely astonishing

  • @AlienValkyrie
    @AlienValkyrie 3 ปีที่แล้ว +1

    holy shit. When he gave the robots example, I thought "hm, yeah, this is pretty nifty, I guess"; then he brought out the OrderedCounter class and mind = blown

  • @evolagenda
    @evolagenda 2 ปีที่แล้ว +2

    The guy with the self / super question had a point. If you inherit from a parent class it's methods are available on self. So super().method() is the same as self.method() at that point. Unless you're calling super().__init__() or something. I guess the point was if you use self it'll spend time looking at the current class for them method first and then go to the parent which I guess if you reimplement it in some way can be a problem, whereas if you use super it will just go straight to the parent.

  • @thanhtan030t
    @thanhtan030t ปีที่แล้ว

    I learn how to use fake over mocking via Robot's example. Amazing!

  • @MrEternalFool
    @MrEternalFool 7 ปีที่แล้ว +8

    Why would you put Raymond's talk and David Beazley talk at the same time?

    • @MattJoinerPwns
      @MattJoinerPwns 7 ปีที่แล้ว

      Which would you choose?

    • @pointerish
      @pointerish 4 ปีที่แล้ว +1

      I'd attend Raymond's just because he's a Core Developer. David is great though.

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน

      I don't know who the other guy is but his talk can't possibly have been any worse than this one

  • @HypocriticalElitist
    @HypocriticalElitist 9 ปีที่แล้ว +10

    Raymond's examples are slightly contrived to illustrate uses of super(). He has a class like CleaningRobot call super().move_forward(), and then uses a subclass to join this logic with an alternative implementation of move_forward(). In real code, it'd make more sense to call self.move_forward() (implicitly inheriting Robot's implementation), and have a subclass override move_forward(), using only a linear inheritance hierarchy.
    super() should only be used in cases where the class containing the super() call *knows* that it wants to punt to the next class in the MRO. Generally, this means you should only use super() within a method foo() to call another class's implementation of foo(); if you want to call bar() instead, then make it self.bar().

    • @Fulmenify
      @Fulmenify 7 ปีที่แล้ว +1

      I know it's a year late, but as far as I understood, the advantage of the use of super in the robot example is if you have multiple different robot implementations. For testing, you can replace the movement code of each robot as shown, without having to override move_forward() etc. in each of the subclasses.

    • @magno5157
      @magno5157 5 ปีที่แล้ว

      @@Fulmenify super() by itself doesn't give you the advantage of dependency injection. What Raymond has done here is to use super() *and* exploit the diamond inheritance pattern for dependency injection without having to override any functions.

    • @vela7447
      @vela7447 5 ปีที่แล้ว

      41:50 covers this?

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน

      super() should never be used, except in cases where there's only single inheritance and it doesn't make any difference. It's fundamentally broken.

  • @paulallen1597
    @paulallen1597 2 ปีที่แล้ว +1

    This makes sense. I actually learned from this talk.

  • @kenshinnanashi9469
    @kenshinnanashi9469 5 ปีที่แล้ว +1

    Very interesting speech. Thanks a lot !

  • @uppubhai
    @uppubhai 8 ปีที่แล้ว +1

    The heck happened in 19:00 .can somebody explain?

  • @nilindef9270
    @nilindef9270 7 ปีที่แล้ว +1

    Really helped. Great talk. Thanks!

  • @Neceros
    @Neceros 9 ปีที่แล้ว +1

    19:40 I had to pause it and really think about what he was saying. My brain just doesn't comprehend this stuff well anymore, but still he makes a lot of sense.

    • @martialblog
      @martialblog 8 ปีที่แล้ว +2

      +Neceros It gets clearer at 30:30 - Children get called before their parents, and parents get called in the order listed. So MeFirst(MeSecond, MeThird).

    • @skepticmoderate5790
      @skepticmoderate5790 4 ปีที่แล้ว

      @@martialblog Isn't this just a preorder traversal, but with the child as the root instead of the parents? Why does he imply that this is some kind of revolutionary idea?

    • @Han-ve8uh
      @Han-ve8uh 4 ปีที่แล้ว

      @@skepticmoderate5790 www.srikanthtechnologies.com/blog/python/mro.aspx Look at case 4 in this article. Pre-order traversal will do D B A C, not the same as MRO. Also case 5 is impossible for MRO but pre-order will be C A B

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน

      It's not your fault -- it's a truly insane anti-feature.

  • @TPHRyan
    @TPHRyan 9 ปีที่แล้ว

    I'm confused, what is a use case for the Root (stopper) class, where you would want to, for example, draw() on EVERY OBJECT in the MRO instead of just stopping at the first?
    To clarify, I mean a use case where there isn't already a definitive "Adam" that would actually do something useful with its method.

  • @rembautimes8808
    @rembautimes8808 4 ปีที่แล้ว

    Excellent communicator

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน

      Too bad what he's communicating is completely wrong.

  • @FunkyELF
    @FunkyELF 8 หลายเดือนก่อน

    Don't know why this was promoted to me just now but I don't like that this video says it's 9 years old and from PyCon 2015. It adds up, but seems like it shouldn't.

  • @ChristophZwerschke
    @ChristophZwerschke 9 ปีที่แล้ว +4

    Beneficial and hilarious as usual - Raymond considered super. The first part was a bit difficult to follow though, without a visual diagram of the family tree. Apart from that, I liked the presentation style using only Emacs.

    • @skepticmoderate5790
      @skepticmoderate5790 4 ปีที่แล้ว +1

      It's just a preorder traversal, but with the child as the root.

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน

      Raymond considered colossally incompetent I believe you mean

  • @hangugeohaksaeng
    @hangugeohaksaeng 9 ปีที่แล้ว +23

    Thanks for the talk Raymond. One note: No one else in that room, or online, is familiar with your family tree. Why not choose characters from a popular movie, or celebrities? Anything else would be better than the relationships almost guaranteed to be known only to you.

    • @morenoh149
      @morenoh149 7 ปีที่แล้ว +2

      to be fair it's pretty close to being the minimal example needed to teach MRO right? It could've been classes A,B,C,D,E,F,G,H,I

    • @laxmanapolisetti
      @laxmanapolisetti 6 ปีที่แล้ว +1

      Even a popular movie characters would be unfamiliar to me. I just replaced with my family tree and it was hell lot clear to me after that. I also drew Raymond family tree to compare. One of the best lecture though, beautifully explained.

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน

      The obfuscation is intentional. If he used a clear example, it might become easy for the audience to realize just how broken multiple inheritance is in python. Can't have that, so we must contrive an example that makes absolutely no sense.

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน

      @@laxmanapolisetti If it was really "beautifully explained", it should've started and ended with the explanation that multiple inheritance is fundamentally broken in python, impossible to use correctly, and that the author of super considered harmful was correct about almost everything (the only error he made was in not making the recommendation to drop MI altogether). He chose to mislead instead.

  • @mishasawangwan6652
    @mishasawangwan6652 2 ปีที่แล้ว

    reminds me of express middleware. in fact, i dare say it’s conceptually the same.

  • @RadCirskis
    @RadCirskis 8 ปีที่แล้ว +1

    Great talk tanks! I am a bit confused with Python documentation on super. It says: "Return a proxy object that delegates method calls to a parent or sibling class of type.". I suspect it means the sibling of a parent, not the sibling of the type. Am I correct?

    • @jebob1
      @jebob1 6 ปีที่แล้ว

      No, it is possible that the method called could be a sibling of the type. The cleaning robot's super points at sibling MockRobot when the robot is a MockCleaningRobot.

    • @magno5157
      @magno5157 5 ปีที่แล้ว

      The term "sibling" shouldn't even be used at all. "Siblings" are obvious in the diamond pattern. However, if you have a very complicated multiple-inheritance tree, it can be very difficult to see which class is a "sibling" of which other classes. The only non-ambiguous way to determine super() is to apply the MRO algorithm as explained in www.python.org/download/releases/2.3/mro/, which turns out to be really easy (although you need to be really careful for complicated trees to avoid careless mistakes).

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน

      The only real way to make sense of this is to never use multiple inheritance in python. It's fundamentally borked and impossible to use correctly. Stick to single inheritance if you must use inheritance at all.

  • @BrendanMetcalfe
    @BrendanMetcalfe 5 ปีที่แล้ว +1

    The master!

  • @dimitristheodorou4959
    @dimitristheodorou4959 9 ปีที่แล้ว

    Um... I just ran this exact code with Python 2 and it does *not* work as advertised. I still get the insecticide treated dough.

    • @dimitristheodorou4959
      @dimitristheodorou4959 9 ปีที่แล้ว +3

      Dimitris Theodorou Nevermind! The OrganicDoughFactory must inherit from DoughFactory for this to work, which I had omitted.

  • @barbarairena6714
    @barbarairena6714 6 ปีที่แล้ว

    in the first (pizza-)example the the MRO gets changed only if a) OrganicPizza- class is created (subclass of Pizza and DoughFactory) and called and if b) the OrganicDoughFactory is created as a subclass of DoughFactory-class! But why b)?? I dont understand the necessity of b)

    • @Han-ve8uh
      @Han-ve8uh 4 ปีที่แล้ว

      This talk did not have time to go under the hood to explain how MRO is working, you can draw out the inheritance diagrams of both the pizza and robot example in this talk and see that they match Case 4 in this article: www.srikanthtechnologies.com/blog/python/mro.aspx. In the talk, he was basically adding 2 more classes to create this diamond, and "hacking" the MRO order so new custom methods are called.

  • @CrystalGamma
    @CrystalGamma 9 ปีที่แล้ว +1

    I think the Python 2 syntax of super() makes it more obvious what is actually happening. That said I only suspected something like this happening under the hood before watching this talk.

  • @geshtu1760
    @geshtu1760 5 ปีที่แล้ว +1

    Seems I'm the first one to point out that if you remove the insecticide, you need some other way of dealing with the insects (and that other way may have a different set of pros and cons too) ;) Awesome talk though, but I actually think this way of rewriting the "supply chain" is horrible because it is too "magic" and easy to cause surprises

    • @geshtu1760
      @geshtu1760 5 ปีที่แล้ว +2

      In fact this is probably a good example of why many coders are starting to turn away from OOP

  • @silence_coder
    @silence_coder 7 ปีที่แล้ว

    Nice Talk

  • @DheerajSharma-ol3go
    @DheerajSharma-ol3go 3 ปีที่แล้ว

    Thankyou sentdex

  • @anibaldk
    @anibaldk 2 ปีที่แล้ว

    I think this guy is considered Super

  • @baksagimm5890
    @baksagimm5890 5 ปีที่แล้ว +1

    wow now i get it. super is super complicated

  • @BryanChance
    @BryanChance 3 ปีที่แล้ว

    Oops. Let me go read the article now..lol I used to hate python until I saw some of his videos .

  • @lrebollo
    @lrebollo 7 หลายเดือนก่อน

    27:56 LOL

  • @nuynobi
    @nuynobi 5 ปีที่แล้ว

    FauxBot would have been a better name than MockBot.

  • @BeYakko1
    @BeYakko1 8 ปีที่แล้ว +2

    "super does not call your parents it calls the ancestors of your children". um, the ancestor of my children would be me, um, the parent. that statement is so confusing. I have watched this over 100 times and I still don't get it.

    • @twistedsim
      @twistedsim 7 ปีที่แล้ว

      That means that it may goes further than just "it's parent"

    • @Lexaire
      @Lexaire 4 ปีที่แล้ว

      super() does not look at the parents of the class it's in. It uses the MRO to find the next in line for whoever created the object. So in a child (subclass) object, if one of the parents calls super(), it will not check the parent's parents but instead look at the object's MRO and see who is next in line. In the case of the family tree, when we have a Matthew object and Dennis calls super(), it goes through the Matthew object's MRO and finds the next in line: Sharon.

  • @stoneshou
    @stoneshou 7 ปีที่แล้ว +2

    Every Raymond talk has one extremely funny joke that I did not know before. In this one: E(xclusivelyusedby)M(iddle)A(ged)C(omputer)S(cientists)..

  • @yukiousuzumi2595
    @yukiousuzumi2595 7 ปีที่แล้ว +4

    What? Can't believe one takes 45 minutes to explain a `super()` call. Let me add this to my favorite list for further watching though.

  • @dunder9444
    @dunder9444 4 ปีที่แล้ว

    Sentdex brought me here

  • @ruthlessadmin
    @ruthlessadmin 9 ปีที่แล้ว +2

    I'm still so fucking confused :'( I may be too drunk for this....

  • @alexpyattaev
    @alexpyattaev 20 วันที่ผ่านมา

    These sort of tricks is exactly why super is harmful

  • @isodoubIet
    @isodoubIet 6 หลายเดือนก่อน +3

    I cannot imagine how much brain matter must be lost before one calls super() "well-designed".

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน +3

      Already in the first 5 minutes we have a glaring error. Multiple inheritance in python _never_ works, because there's no such thing as "classes without overlapping methods", because __init__ always overlaps, which means it's impossible to call __init__ correctly, which means it's flatly impossible to use multiple inheritance correctly in python. The author of super considered harmful understood this. You don't. Drop the smug and try to learn something.

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน +3

      Second glaring error, C3 linearization is not a "best and optimum solution to this problem", because it's not a solution to this problem in any sense. Turning multiple inheritance into single inheritance means there's no multiple inheritance at all, so you didn't solve the problem, so the thing doesn't. fricking. work. Funnily enough, multiple inheritance in C++ works just fine. You can use it. It works. You don't have to make all the classes aware of one another and demolish encapsulation to make it work. The author of super considered harmful understood this. You don't. Drop the smug and try to learn something.

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน +3

      More glaring errors I didn't bother to mention, but Pizza inheriting from DoughFactory? Come on.

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน +3

      No, it's not awesome that you can change the parent from the child, actually. It's insane, brittle, and impossible to use correctly. "is this fragile? no, it's deterministic" so is everything in programming. It's _extremely_ brittle.
      That's all the incompetence I can stomach, I'm afraid.

  • @beep_doop
    @beep_doop 8 ปีที่แล้ว +1

    a pizza-place is not a kind of dough-factory so it shouldnt inherit from it

    • @spencer3752
      @spencer3752 8 ปีที่แล้ว +5

      The purpose of subclassing is for code reuse. It doesn't matter if the subclass is not the same "kind" as the parent class. And in a way, a pizza place is certainly a specialized form of a dough factory. They produce an item, which is made from dough. Need to take a look at his "Python Class Development Toolkit" video and "Art of Subclassing" video.

    • @internetzpotato383
      @internetzpotato383 4 ปีที่แล้ว

      In this case the factory is thought of as a mixin type, not a base class you are inheriting from. It'd probably be clearer if it was called `DoughFactoryMixin`.

  • @aaronbell5994
    @aaronbell5994 8 ปีที่แล้ว

    :)

  • @SuperAhmed1337
    @SuperAhmed1337 8 ปีที่แล้ว +13

    I only wished he didn't use references to a topic he has no clue about.
    "Organic". "Pesticides". Blah, blah.

    • @litbmeinnick
      @litbmeinnick 5 ปีที่แล้ว +2

      How can you know he's no clue about it?

    • @internetzpotato383
      @internetzpotato383 4 ปีที่แล้ว +1

      @@litbmeinnick Duh, because he doesn't have the same opinion as the complainer, a clear sign he is not well-informed.

    • @isodoubIet
      @isodoubIet 6 หลายเดือนก่อน +1

      That would mean no talk because he has no clue about anything.

    • @SuperAhmed1337
      @SuperAhmed1337 5 หลายเดือนก่อน

      @@internetzpotato383 i've studied this, but do go on...

  • @nicholasdewaal
    @nicholasdewaal 5 ปีที่แล้ว +1

    It's extremely ironic that someone doing so much awesome work in advancing tech spends half this talk beating the Luddite drum of not modifying the software/code in plants (GMO's). Honestly, adding vitamins to rice as a GMO (golden rice) could save millions of children from death and malnutrition. I love Raymond, but stop the Luddism, and stop risking the lives of children please. Your talk about super is harmful.

    • @narnbrez
      @narnbrez 5 ปีที่แล้ว +1

      i think he just uses it as a hypothetical. if you were designing a system for a customer and the customer wanted a particular type of widget, the system should give that particular type of widget.
      can you link to where he actually supports anti-gmo positions? He spends more time talking about insecticide than GMOs. (granted GMOs and insecticide are related since some strains are insecticide resistant and thus can be treated with higher strength insecticide without damaging the crop, although that has negative side effects for the ecosystem and neighboring farms not using the same strain of gmo. he does not make that connection himself anywhere though.)
      Given how candidly he remarked on how bell peppers are obviously healthier for you than pepperoni, I think if he actually was anti-GMO he would have stated it. It was merely a (largely) non-offensive way of constructing a hypothetical real-world example to demonstrate a concept.
      i consider myself a fierce advocate for gmo foods but i dont see your point.

    • @internetzpotato383
      @internetzpotato383 4 ปีที่แล้ว

      Business guy: You have to change the production code, we stand to lose millions!
      Developer: Nobody understands this legacy code after you fired the old team. It's been there for years and it's too dangerous to change without understanding more. There is much more that we do not understand than understand at this point.
      Business guy: If we lose millions, we will have to start laying off people. They all have families, for God's sake! Think of the CHILDREN!!!