Python's Class Development Toolkit

  Рет қаралды 237,488

Next Day Video

Next Day Video

Күн бұрын

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
@edmartian
@edmartian 4 жыл бұрын
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.
@lionpersia
@lionpersia 6 жыл бұрын
Reymond Hettinger and James Powell: I owe a great deal to these two men.
@SanataniAryavrat
@SanataniAryavrat 4 жыл бұрын
agree... 100%
@l_combo
@l_combo 4 жыл бұрын
+ David Beazly!
@AlbertKamu
@AlbertKamu 11 жыл бұрын
One of the best performances on PyCon USA I've seen. Thanks a lot for upload!
@ameyms
@ameyms 10 жыл бұрын
"Who learned something new?" *I* did.
@justgivemethetruth
@justgivemethetruth 9 жыл бұрын
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 жыл бұрын
Not me, I'm drunk. I think this lecture calls for being sober...
@nishankbani3257
@nishankbani3257 6 жыл бұрын
Raymond Hettinger style of delivery is fantastic and the content is very good delivered in a succinct manner.
@pavelivanov957
@pavelivanov957 3 жыл бұрын
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!
@mountainhobo
@mountainhobo 10 жыл бұрын
Brilliant delivery. Whether you are interesting in programming or not, it's worth watching, if just for the overall business process implications.
@AdmiralMaur
@AdmiralMaur 9 жыл бұрын
This was so entertaining that I'll swap out my movie tonight for this video. Thanks!
@ipsectunnel
@ipsectunnel 11 жыл бұрын
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 Жыл бұрын
That's *what* part of his signature? You seem to be referring to something that was not actually said.
@Nathan-pl2cf
@Nathan-pl2cf Жыл бұрын
@@rodrigobraz2 The comment is from 9 years ago. It was probably before replies were a thing
@wduandy
@wduandy Жыл бұрын
@@Nathan-pl2cf 😔
@justgivemethetruth
@justgivemethetruth 9 жыл бұрын
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.
@shashvatshukla
@shashvatshukla 7 жыл бұрын
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.
@w.r.7440
@w.r.7440 4 жыл бұрын
Such a talented speaker, really interesting presentation to watch
@jacobblanton7982
@jacobblanton7982 10 ай бұрын
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.
@CampaignsIlraon
@CampaignsIlraon 7 жыл бұрын
This, is, incredibly thorough. Thanks so much.
@tomislater
@tomislater 11 жыл бұрын
Great video. Thanks Raymond.
@akypar
@akypar 10 ай бұрын
Having seen this video up to 20', I could easily replace Raymond's lectures over my netflix subscription
@brum293
@brum293 11 жыл бұрын
I think it's good. :) It gives Raymond feedback so he can adapt the talk to the technical level of the audience.
@OrotundBucket
@OrotundBucket 4 жыл бұрын
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
@krasoffski
@krasoffski 11 жыл бұрын
Awesome! Than you, Raymond.
@Anshumantiwarianstiwar
@Anshumantiwarianstiwar Жыл бұрын
Wow, this was really amazing, I definitely learned something new today.
@salkdjfasldkfjsdlk
@salkdjfasldkfjsdlk 9 жыл бұрын
Great talk. Everyone I share this with gets a kick and yes, they learn something new.
@MsSuyash1995
@MsSuyash1995 5 жыл бұрын
This was a fantastic talk...
@dhaval1489
@dhaval1489 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 жыл бұрын
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.
@brianseroney854
@brianseroney854 9 жыл бұрын
thanks Ray i love it it's awesome brother bigup
@DavidCodes
@DavidCodes 4 жыл бұрын
amazing. learned something new!
@trombonemunroe
@trombonemunroe 10 жыл бұрын
This is a must-see video for Pythonistas.
@jimstanley_49
@jimstanley_49 7 жыл бұрын
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.
@swyxTV
@swyxTV 4 жыл бұрын
i dont even use python regularly and i watched this all the way through
@lizzie0196
@lizzie0196 9 жыл бұрын
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 жыл бұрын
+John This is great, John. Thanks a bunch !
@EmanueleCagliero
@EmanueleCagliero 7 жыл бұрын
Great work +John! I made some changes to adapt it to Python 3: github.com/EmanueleCagliero/py_class_toolkit
@hololightful
@hololightful 7 жыл бұрын
John unfortunately, that link is dead now... but the link in the reply below on GitHub is working...
@MrTigerstyle80
@MrTigerstyle80 4 жыл бұрын
Flippin Big RH absolutely doing WORK back in the day. Guy’s a beast am I right?’
@IanSebryk
@IanSebryk 11 ай бұрын
this was amazing. can't believe i hadn't come across this before. and i feel like an idiot for not having done so.
@jasmeet17mast
@jasmeet17mast 3 жыл бұрын
Great video/content thanks for recording it in 480p! 2013
@BeYakko1
@BeYakko1 7 жыл бұрын
I learned something new
@karsten1405
@karsten1405 5 жыл бұрын
That moment when Raymond goes "..the opposite of privacy - [...] freedom" 37:42
@FredDamstra
@FredDamstra 5 жыл бұрын
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.
@ordinarygg
@ordinarygg 11 жыл бұрын
Must see for every Python programmer))
@mirageowl
@mirageowl 4 жыл бұрын
I like how this makes me able to use Python just like Ruby
@DrDeuteron
@DrDeuteron 11 ай бұрын
17:30 so classic python. Now we have ppl demanding type ints. I do not feel icky not knowing what types are being passed.
@stoneshou
@stoneshou 7 жыл бұрын
Raymond: if you wish hard enough, the fairy godmother appears .. I guess we all wished hard enough and so Raymond appeared
@dariofagotto4047
@dariofagotto4047 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
@kronosaurelius
@kronosaurelius 5 жыл бұрын
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__).
@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
@etherwar
@etherwar 7 жыл бұрын
I learned several new things, but it's the "proleptic gregorian calendar", not "apoplectic" or whatever he was trying to say :)
@justgivemethetruth
@justgivemethetruth 9 жыл бұрын
Kind of weird how much this guy looks like Ray Liotta.
@willd0g
@willd0g 5 жыл бұрын
“TRUE OR FALSE: is this my first Rodeo?”
@JonMW
@JonMW 3 жыл бұрын
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"? :-)
@ChillingSpartan
@ChillingSpartan 5 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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.
@CC-sr4zi
@CC-sr4zi 7 жыл бұрын
I learned sth new
@mpgovinda
@mpgovinda 4 жыл бұрын
If __init__ isn't a constructor. What's the actual constructor of python, is it __new__?
@buybuydandavis
@buybuydandavis 3 жыл бұрын
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):"
@Daco812
@Daco812 4 жыл бұрын
36:25 - 37:53 .__ == freedom
@Tech-jc5gy
@Tech-jc5gy Жыл бұрын
7:06 10:26 coding a reusable vs constant
@onlybrad8434
@onlybrad8434 6 жыл бұрын
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 жыл бұрын
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...
@tc14hd23
@tc14hd23 Жыл бұрын
What does he say at 11:13? "and upload it to ???"
@akshaydeshpande7382
@akshaydeshpande7382 6 жыл бұрын
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
@mcan543
@mcan543 4 жыл бұрын
#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 жыл бұрын
George Clooney of Python!
@anibaldk
@anibaldk 4 жыл бұрын
39:31 - shouldn't __init__ do *self.diameter* = radius * 2.0 ? Otherwise, you're violating your own ISO-222220
@Asdayasman
@Asdayasman 9 жыл бұрын
Camera guy fall asleep at the end or something? Show me the fucking slides, damnit!
@akshaydeshpande7382
@akshaydeshpande7382 6 жыл бұрын
here is the link for slides : speakerdeck.com/pyconslides/pythons-class-development-toolkit-by-raymond-hettinger
@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.
@solaxun
@solaxun 7 жыл бұрын
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 жыл бұрын
Mark Woodworth Came here for that comment :p !
@twistedsim
@twistedsim 7 жыл бұрын
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 жыл бұрын
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
@fridyair
@fridyair 4 жыл бұрын
can't do on a compiled language? swift does it. probably a bit better too.
@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 жыл бұрын
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 жыл бұрын
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.
@AyuPapaBonanza
@AyuPapaBonanza 6 жыл бұрын
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 жыл бұрын
hah, "consenting adult's language"
@ZapOKill
@ZapOKill 4 жыл бұрын
who needs more then 6 decimal_digits of pi?
@tissuepaper9962
@tissuepaper9962 2 жыл бұрын
That's not the point.
@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.
@futurepython6774
@futurepython6774 2 жыл бұрын
python/django/mysql新教程: python基础:kzfaq.info/get/bejne/nZyCobaDqdDIZYE.html
@1over137
@1over137 2 жыл бұрын
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.
@FlashManinSpace
@FlashManinSpace 9 жыл бұрын
I learned nothing new from this.
@guerra_dos_bichos
@guerra_dos_bichos 8 жыл бұрын
+Flash Man good for you!
@wanderingfido
@wanderingfido 5 жыл бұрын
Wrong...you CAN make the instance variables private by prefixing them with two underscores: *self.__radius*.
@karelpeeters6240
@karelpeeters6240 5 жыл бұрын
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.
@MrTweetyhack
@MrTweetyhack 8 жыл бұрын
_init_. haha. python is a good joke.
@funprog
@funprog 11 жыл бұрын
Excellent, but stop saying "who learned something new" its tedious
@BikashBiswalCHHUAPUA
@BikashBiswalCHHUAPUA 6 жыл бұрын
Only theory demonstration. It's a very boring class.
Stop Writing Classes
27:29
Next Day Video
Рет қаралды 932 М.
Transforming Code into Beautiful, Idiomatic Python
48:51
Next Day Video
Рет қаралды 1 МЛН
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 85 МЛН
Finger Heart - Fancy Refill (Inside Out Animation)
00:30
FASH
Рет қаралды 29 МЛН
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 65 МЛН
The Mental Game of Python - Raymond Hettinger
1:10:00
SF Python
Рет қаралды 123 М.
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,6 МЛН
The Flaws of Inheritance
10:01
CodeAesthetic
Рет қаралды 921 М.
The Clean Architecture in Python
49:54
Next Day Video
Рет қаралды 109 М.
Keynote
42:57
Next Day Video
Рет қаралды 30 М.
Object-Oriented Programming is Bad
44:35
Brian Will
Рет қаралды 2,3 МЛН
When Booleans Are Not Enough... State Machines?
21:50
Next Day Video
Рет қаралды 24 М.
Raymond Hettinger, Keynote on Concurrency, PyBay 2017
1:13:53
SF Python
Рет қаралды 150 М.
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН