Python's Class Development Toolkit

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ส.ค. 2024
  • Raymond Hettinger
    This is a short, but thorough tutorial on the Python's built-in toolset for creating classes. We look at commonly encountered challenges and how to solve them using Python.

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

  • @ameyms
    @ameyms 10 ปีที่แล้ว +113

    "Who learned something new?"
    *I* did.

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

      Amey Sakhadeo
      All I learn from this kind of video is just how clueless I am about the guts of a language and how hard it is to focus on on WTF he is talking about! ;-)

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

      Not me, I'm drunk. I think this lecture calls for being sober...

  • @lionpersia
    @lionpersia 6 ปีที่แล้ว +100

    Reymond Hettinger and James Powell: I owe a great deal to these two men.

  • @edmartian
    @edmartian 4 ปีที่แล้ว +10

    I have been programming regularly in Python for a few years and I keep watching this Video. Again and again I get something new from it.

  • @jacobblanton7982
    @jacobblanton7982 10 หลายเดือนก่อน +1

    Every programming class should be taught this way. I love how this is implemented in a project-style manner. I wish I learned OOP this way.

  • @nishankbani3257
    @nishankbani3257 6 ปีที่แล้ว +5

    Raymond Hettinger style of delivery is fantastic and the content is very good delivered in a succinct manner.

  • @AlbertKamu
    @AlbertKamu 11 ปีที่แล้ว +17

    One of the best performances on PyCon USA I've seen.
    Thanks a lot for upload!

  • @akypar
    @akypar 10 หลายเดือนก่อน

    Having seen this video up to 20', I could easily replace Raymond's lectures over my netflix subscription

  • @ipsectunnel
    @ipsectunnel 11 ปีที่แล้ว +50

    That's part of his "signature", I actually took almost 80 hours of training from Raymond Hettinger and that's one of the teaching techniques he continuously uses throughout his classes to reinforce ideas and concepts, and let me tell you it is very effective, definitely one of the best courses I've ever taken.

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

      That's *what* part of his signature? You seem to be referring to something that was not actually said.

    • @Nathan-pl2cf
      @Nathan-pl2cf ปีที่แล้ว +12

      @@rodrigobraz2 The comment is from 9 years ago. It was probably before replies were a thing

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

      @@Nathan-pl2cf 😔

  • @pavelivanov957
    @pavelivanov957 3 ปีที่แล้ว +5

    Thank you for excellent explanation of what certain functions are meant for in Python and not just explaining how they work! Just cleared out a lot of very important topics! Just great!

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

    This was so entertaining that I'll swap out my movie tonight for this video. Thanks!

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

    This guy is a great teacher ... at least 8 minutes into the video. I like what he has to say ... and always good when you hear what lots of people think that is wrong too.

  • @OrotundBucket
    @OrotundBucket 4 ปีที่แล้ว +8

    God damn it, I almost spit out my coffee when he said, "Reproducible results? Oh, that's a new feature in science. How often do you get that?" 16:04

  • @w.r.7440
    @w.r.7440 4 ปีที่แล้ว +6

    Such a talented speaker, really interesting presentation to watch

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

    i dont even use python regularly and i watched this all the way through

  • @mountainhobo
    @mountainhobo 10 ปีที่แล้ว +54

    Brilliant delivery. Whether you are interesting in programming or not, it's worth watching, if just for the overall business process implications.

  • @CampaignsIlraon
    @CampaignsIlraon 7 ปีที่แล้ว +11

    This, is, incredibly thorough. Thanks so much.

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

    Great talk. Everyone I share this with gets a kick and yes, they learn something new.

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

    Great video. Thanks Raymond.

  • @krasoffski
    @krasoffski 11 ปีที่แล้ว

    Awesome! Than you, Raymond.

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

    I think it's good. :) It gives Raymond feedback so he can adapt the talk to the technical level of the audience.

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

    Wow, this was really amazing, I definitely learned something new today.

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

    I am a python novice and not got a hold on class (OOP) most of whatever you said in pure technical sense went right over my head but the delivery and style, I am fan now and I think now I will be Ninja when I start OOP

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

      Dhaval Shah , I watched this video 5 years ago. I did not understand most of the lecture. but I knew I found a great teacher. I just watched it today, for the first time in five years,and I could follow almost everything and I learned something new. Best wishes , and happy coding.

  • @shashvatshukla
    @shashvatshukla 7 ปีที่แล้ว +14

    Wow this was an amazing lecture! Great for someone who knows some python, and wants to learn all about classes in Python.
    Apart from the summary slide at 45:00, some of the main things I learnt were:
    1) See how people use your code (through fast feedback loops), and make sure new changes don't break their code. Think especially about people who will make subclasses from your classes. Expect that there is a good way to meet new requirements while not breaking old user code
    2) Add just what you need as part of the Agile process. It seems that Python is great for Agile development because you can get up and running with very simple code, and add more functionality in the future without changing the API.
    3) Python is different from Java or C++. I guess each language has their own style, and it is a good idea to learn to do things the right way in any language you learn, rather than bring the old habits along.

  • @trombonemunroe
    @trombonemunroe 10 ปีที่แล้ว +19

    This is a must-see video for Pythonistas.

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

    This was a fantastic talk...

  • @jimstanley_49
    @jimstanley_49 7 ปีที่แล้ว +3

    I learned something new!
    Now I have to go change all the getters and setters I thought I needed because I wanted to do more than just store the property.

  • @IanSebryk
    @IanSebryk 11 หลายเดือนก่อน +1

    this was amazing. can't believe i hadn't come across this before. and i feel like an idiot for not having done so.

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

    amazing. learned something new!

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

    Here are the contents of the slides with comments in the form of an IPython Notebook.
    nbviewer.ipython.org/urls/dl.dropboxusercontent.com/u/5095342/IPython%20Notebooks/Python%27s%20Class%20Development%20Toolkit.ipynb

    • @samarth.rangavittal
      @samarth.rangavittal 8 ปีที่แล้ว +1

      +John This is great, John. Thanks a bunch !

    • @EmanueleCagliero
      @EmanueleCagliero 7 ปีที่แล้ว +6

      Great work +John! I made some changes to adapt it to Python 3:
      github.com/EmanueleCagliero/py_class_toolkit

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

      John unfortunately, that link is dead now... but the link in the reply below on GitHub is working...

  • @karsten1405
    @karsten1405 5 ปีที่แล้ว +7

    That moment when Raymond goes "..the opposite of privacy - [...] freedom" 37:42

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

    thanks Ray i love it it's awesome brother bigup

  • @DrDeuteron
    @DrDeuteron 11 หลายเดือนก่อน +1

    17:30 so classic python. Now we have ppl demanding type ints. I do not feel icky not knowing what types are being passed.

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

    Flippin Big RH absolutely doing WORK back in the day. Guy’s a beast am I right?’

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

    Great video/content
    thanks for recording it in 480p! 2013

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

    I like how this makes me able to use Python just like Ruby

  • @BeYakko1
    @BeYakko1 7 ปีที่แล้ว +18

    I learned something new

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

    python/django/mysql新教程:
    python基础:th-cam.com/video/g6RnSRDjd5M/w-d-xo.html

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

    Must see for every Python programmer))

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

    Raymond: if you wish hard enough, the fairy godmother appears .. I guess we all wished hard enough and so Raymond appeared

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

    Would area() be better as an @property, as well? Seems like that would be a more python-like implementation and would make the calling code easier.

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

    "Properties are something you can do in a dynamic language but not in compiled ones"... Unless you use turbopascal

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

      Or Ada

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

    I learned several new things, but it's the "proleptic gregorian calendar", not "apoplectic" or whatever he was trying to say :)

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

    Is there any book with this kind of explanation? These videos are best tutorial.

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

      Krund
      Effective Python by Brett Slatkin has a similar type of explanations but less in depth then this lecture

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

    I thought I knew how to make a class in python. This video showed me that I know nothing.

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

      Is your last name "Snow"? :-)

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

    Code is read more than is written.
    So next time you are reading a piece of code and see
    obj.radius = 3
    Do you know if you are setting a variable or calling a function?

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

      Does it really matter? Accessing a member variable in Python is a function call anyway (__getattr__, __setattr__).

  • @willd0g
    @willd0g 5 ปีที่แล้ว +6

    “TRUE OR FALSE: is this my first Rodeo?”

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

    If __init__ isn't a constructor. What's the actual constructor of python, is it __new__?

  • @Tech-jc5gy
    @Tech-jc5gy ปีที่แล้ว

    7:06
    10:26 coding a reusable vs constant

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

    Kind of weird how much this guy looks like Ray Liotta.

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

    I really can't see the problem with self.perimeter() methode inside area()... this is what I would have done in another programming language like Java because "this" will always use the method of the class of the object if it exist

    • @AJpennster
      @AJpennster 6 ปีที่แล้ว +3

      The problem is that when an instance of Tire calls self.area(), self will refer to the tire instance. So inside the area method, self = Tire instance. Therefore when self.perimeter is called, python checks for a perimeter attribute inside of the Tire instance first, which it will find and use; however, that is the extended version of perimeter so the area would be different than expected.

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

      @@AJpennster but for Tire instance it should take the extended perimeter method??? Inside the Tire class the perimeter method returns Circle.perimeter(self) * 1,25. So what's wrong? I can't see the problem, too...

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

    The first two things I did understand. After that I didn't understand anything. Is there a way to learn what subclasses are and when to use, what instanciate really means and when to use these class methods and static methods?

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

      Sparta Best way to learn, is to experiment - perhaps in the python shell.
      Consider a class to be like a factory mould (a form) that you pour molten metal into to create an 'instance' of whatever you are manufacturing. Every instance is the same as every other instance, the same as the class, but what you do with them is up to you. (Perhaps if the mould if for a cooking pan, you cook very different things in each - they end up containing different ingredients, or attributes in Python terminology.)
      A sub-class is something that inherits all of the characteristics of the parent class but then has some differences. Imagine a class for pets. Cats and dogs share many characteristics (fur, four legs, etc) but make different sounds (one meows, the other barks). You use one class for everything that is the same, and sub-classes for the things that are different (e.g. food, exercise, training, sounds, etc.).
      As described in the video, a static method is simply a function included in a class definition for convenience. Instead of just writing func(parameters) you write class.func(parameters) when you use it, avoiding any confusion with any other function of the same name that might do something different.

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

      Stuart - Well, I think I understand. For example I could build a class 'Vehicle' with some variables likes tires = 4 and doors = 2 and lights = 2. I also could build some functions like 'accelerate' and 'brake'. After I build all I could use this class as a car or a subclass 'truck' with tires = 6. Is that right?

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

      Yes.
      Note that functions that refer to the instance (by convention, using self) are called methods and variables that are part of an instance or class are called attributes.
      I strongly recommend you just experiment in the python interactive shell. Then you get instant feedback pin what works and what doesn't.

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

      Check out the OOP tutorials from Corey Schafer here on youtube. They are clear and to the point. Helped me understand pretty much all the points you mentioned. (subclasses, class methods, static methods and more).

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

      guyindisguise - Thanks a lot. I already watched two videos from Corey and they are really great. His videos are even better to understand than videos in my own native language.

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

    36:25 - 37:53 .__ == freedom

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

    What does he say at 11:13? "and upload it to ???"

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

    39:31 - shouldn't __init__ do *self.diameter* = radius * 2.0 ? Otherwise, you're violating your own ISO-222220

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

    can't do on a compiled language? swift does it. probably a bit better too.

  • @CC-sr4zi
    @CC-sr4zi 7 ปีที่แล้ว

    I learned sth new

  • @solaxun
    @solaxun 7 ปีที่แล้ว +6

    I think the final few examples (39:45ish) using @property and @setter are incorrect because the self.radius should be self._radius, otherwise the init will try to call the setter for radius, and since no diameter is bound, it will error.

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

      Mark Woodworth Came here for that comment :p !

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

      I tought of that and I think it will work since the setter is always call when the object is initialized. The getter is never called.

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

      It works. Calling self.radius=radius in __init__ calls the setter, and initializes the diameter

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

      Yeah - I'm not sure what I was originally confused about here... @@MrHaste12

  • @mcan543
    @mcan543 4 ปีที่แล้ว +3

    #KeyTakeAways
    7:15 If you don't have variables don't belong to that instance use class variables. Not instance variables.
    7:55 __init__ is not constructor. It is initializer.
    9:00 Python classes are not similar to the C++ or Java classes. They are more like namespaces or dictionaries.
    11:48 YANGNI
    18:47 Exposing variables and changing them outside of the class is common and normal in Python world.
    21:30 When you create an object and call a attribute or method ( including __init__ ) Python first looks called class itself. Then looks parent classes recursively.
    21:56 Extending and overriding.
    26:26 Alternative constructors with @classmethod decorator
    32:00 @staticmethod decorator
    33:50 Creating a private variable and assign the method to that private variable. It is a good way of protecting your subclasses.
    35:35 Better way of protecting your subclasses assigning the method to a dundervariable.
    39:26 Accessors and mutators. When you set circle.radius = 5, @radius.setter decorated function runs ----> www.python-course.eu/python3_properties.php
    41:44 __slots__
    and lastly, how to do an engaging presentation.

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

    George Clooney of Python!

  • @buybuydandavis
    @buybuydandavis 3 ปีที่แล้ว +4

    Summary: Toolset for New-Style Classes
    1. Inherit from *object()*
    2. *Instance Variables* for information unique to an instance.
    3. *Class Variables* for data shared among all instances.
    4. *Regular Methods* need "self" to operate on instance data.
    5. *Class Methods* implement alternate constructors. They need "cls" so they can create subclass instances as well.
    6. *Static Methods* attach functions to classes. They don't need either "self" or "cls". Static methods improve discoverabiity and require context to be specified.
    7. *@property* lets getter and setter methods be invoked automatically by attribute access. This allows Python classes to freely expose their instance variables.
    8. *__slots__* variable implements the Flyweight Design Pattern by suppressing instance dictionaries.
    9. *__DoubleUnders__* for class level references.

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

      you don't need to inherent from object in python 3 now. writing "class NameOfTheClass:" implicitly does "class NameOfTheClass(object):"

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

    When switching from radius to diameter, doesn't he need to change his __init__ method to `self.diameter = radius * 2.0`?
    (Especially after settings __slots__ = ['diameter'].)

    • @Alex-om6zq
      @Alex-om6zq 7 ปีที่แล้ว +3

      No. Since radius is property, when radius occurs on the left side of assignment, setter is called and result of expression on the right side of assignment passed as second argument (getter is called when property occurs on the right side of assignment). So result of executing is the same as `self.diameter = radius * 2.0`. But using properties we don't need change every place where radius is used.

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

      oic! thanks!
      Do you need the @radius.setter annotation for that to work?

    • @Alex-om6zq
      @Alex-om6zq 7 ปีที่แล้ว +2

      Yes, that's where all the magic happens). There is more information at official docs: docs.python.org/3/library/functions.html#property. Worth mentioning, properties are descriptors: docs.python.org/3.6/howto/descriptor.html.

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

    ok I'm confused (i'm very new to python), I understand that you should use reusable components, but what's the point of using Math.pi? It wont ever change (can I modify it on a large scale inside Python?) and if I ever need to have higher or lower precisions I'd have to rewrite the application all the same, wouldn't it make more sense to just have a Pi object where we could both have the hability fiddle with its precision without adding overhead?

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

      ty :)

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

      he said that on different platforms math.pi is calculated to different precision. for example on a cray it is 128 bit.

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

    Camera guy fall asleep at the end or something?
    Show me the fucking slides, damnit!

    • @akshaydeshpande7382
      @akshaydeshpande7382 6 ปีที่แล้ว +3

      here is the link for slides : speakerdeck.com/pyconslides/pythons-class-development-toolkit-by-raymond-hettinger

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

    A great reddit thread discussion on the same : www.reddit.com/r/Python/comments/2zp0jy/probably_the_best_lecture_ive_seen_raymond/?st=jbsytpb3&sh=deb8db8c

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

    Great talk! I wrote down the code in the talk here www.lvguowei.me/post/python-class-toolkit/ for easy reference.

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

    He's like a way smarter Norm Macdonald

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

    hah, "consenting adult's language"

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

    who needs more then 6 decimal_digits of pi?

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

    "Is python about privacy? No is a consenting adult language" says after explaining how to workaround the fact that python does not have private and protected methods and hack a private method
    "There must be a better way" Yea, real private and protected methods.

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

    I still find it amusing that Python programmers actually think they write real code. They actually think python is object orientated or functional. Python is none of those things, it's a script language. It's for solving problems too simple to bother a real language and project with.
    __init__ cannot initialise instance variables because python doesn't really have instance variables. It has a "self" thing with a dictionary in it. Initialise just adds a dict entry. You could of course just have set it yourself in the members dict. It's not a class, it's a dictionary and a bunch of function pointers.
    It can't be OOP as I don't think it correctly implements any of the corner stones of OOP, so just can't be classed as an OOP language. I mean PHP's OOP implementation was probably more complete than Pythons. It's only OOP to stop OOP programmers whining.
    It suffers the same as all "next generation" language suffer. It starts out with a clear, clean, simple ethos. It's successful and get adopted enough to survive. Then immediately all the people who adopted it start trying to use it to do everything... because oddly a lot of programmers strive to be single tool, single trick ponies and so they try and adapt their language of choice well outside of it's original ethos, add a bunch of junk which basically breaks the ethos and it becomes a bloated mess.
    I'll give you 3 reasons nobody uses Python for anything serious.
    Dynamic typing - untestable.
    Interrupter language - untestable.
    Once your application gets to a certain size, python becomes unmaintainable. Once your data/processing gets to a certain size python becomes non-performant.
    It's fine for it's intended purpose as a "quick", "convenient", "intuitive" utility language. Maybe a new Perl.
    Watching python programmers bigging themselves and their language up on the grand scheme of software engineering is actually quite cute. Awww.

    • @u.rossburton1972
      @u.rossburton1972 ปีที่แล้ว

      I come from a C++ background. I wouldn't say Python is horrible, but there are some real gotchas that come from its design. Like you said, great for small programs or scripts, but there are too many exceptions to the rule and inconsistencies for my taste. If it weren't for that fact that so many employers ask for Python, I would have dropped it after I learned about the issues with it.

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

    Wrong...you CAN make the instance variables private by prefixing them with two underscores: *self.__radius*.

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

      He specifically says that's not what they're intended for at 37:28, and they don't even work, people can still access them if they really want.

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

    I learned nothing new from this.

  • @funprog
    @funprog 11 ปีที่แล้ว +5

    Excellent, but stop saying "who learned something new" its tedious

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

    _init_. haha. python is a good joke.

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

    Only theory demonstration. It's a very boring class.