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.
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!
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.
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.
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! ;-)
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.
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
#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.
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.
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.
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?
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?
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.
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?
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.
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).
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.
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
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.
@@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...
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?
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.
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.
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'].)
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.
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.
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
"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.
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.
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.
Reymond Hettinger and James Powell: I owe a great deal to these two men.
agree... 100%
+ David Beazly!
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.
That's *what* part of his signature? You seem to be referring to something that was not actually said.
@@rodrigobraz2 The comment is from 9 years ago. It was probably before replies were a thing
@@Nathan-pl2cf 😔
@@Nathan-pl2cfThe previous commenter had said 'excellent, but stop saying "who learned something new" it's tedious'
One of the best performances on PyCon USA I've seen.
Thanks a lot for upload!
Raymond Hettinger style of delivery is fantastic and the content is very good delivered in a succinct manner.
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!
Having seen this video up to 20', I could easily replace Raymond's lectures over my netflix subscription
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.
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.
"Who learned something new?"
*I* did.
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! ;-)
Not me, I'm drunk. I think this lecture calls for being sober...
Brilliant delivery. Whether you are interesting in programming or not, it's worth watching, if just for the overall business process implications.
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
Such a talented speaker, really interesting presentation to watch
This was so entertaining that I'll swap out my movie tonight for this video. Thanks!
this was amazing. can't believe i hadn't come across this before. and i feel like an idiot for not having done so.
I think it's good. :) It gives Raymond feedback so he can adapt the talk to the technical level of the audience.
This is a must-see video for Pythonistas.
This, is, incredibly thorough. Thanks so much.
That moment when Raymond goes "..the opposite of privacy - [...] freedom" 37:42
17:30 so classic python. Now we have ppl demanding type ints. I do not feel icky not knowing what types are being passed.
type hints are the best
i dont even use python regularly and i watched this all the way through
Wow, this was really amazing, I definitely learned something new today.
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.
Great talk. Everyone I share this with gets a kick and yes, they learn something new.
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
+John This is great, John. Thanks a bunch !
Great work +John! I made some changes to adapt it to Python 3:
github.com/EmanueleCagliero/py_class_toolkit
John unfortunately, that link is dead now... but the link in the reply below on GitHub is working...
#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.
Flippin Big RH absolutely doing WORK back in the day. Guy’s a beast am I right?’
Great video/content
thanks for recording it in 480p! 2013
amazing. learned something new!
This was a fantastic talk...
I learned something new
Great video. Thanks Raymond.
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.
you don't need to inherent from object in python 3 now. writing "class NameOfTheClass:" implicitly does "class NameOfTheClass(object):"
I like how this makes me able to use Python just like Ruby
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.
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.
Mark Woodworth Came here for that comment :p !
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.
It works. Calling self.radius=radius in __init__ calls the setter, and initializes the diameter
Yeah - I'm not sure what I was originally confused about here... @@MrHaste12
Raymond: if you wish hard enough, the fairy godmother appears .. I guess we all wished hard enough and so Raymond appeared
Must see for every Python programmer))
7:06
10:26 coding a reusable vs constant
Awesome! Than you, Raymond.
thanks Ray i love it it's awesome brother bigup
39:31 - shouldn't __init__ do *self.diameter* = radius * 2.0 ? Otherwise, you're violating your own ISO-222220
"Properties are something you can do in a dynamic language but not in compiled ones"... Unless you use turbopascal
Or Ada
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?
Does it really matter? Accessing a member variable in Python is a function call anyway (__getattr__, __setattr__).
What does he say at 11:13? "and upload it to ???"
I thought I knew how to make a class in python. This video showed me that I know nothing.
Is your last name "Snow"? :-)
36:25 - 37:53 .__ == freedom
If __init__ isn't a constructor. What's the actual constructor of python, is it __new__?
python/django/mysql新教程:
python基础:ruclips.net/video/g6RnSRDjd5M/видео.html
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?
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.
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?
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.
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).
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.
Is there any book with this kind of explanation? These videos are best tutorial.
Krund
Effective Python by Brett Slatkin has a similar type of explanations but less in depth then this lecture
Kind of weird how much this guy looks like Ray Liotta.
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
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.
@@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...
I learned several new things, but it's the "proleptic gregorian calendar", not "apoplectic" or whatever he was trying to say :)
“TRUE OR FALSE: is this my first Rodeo?”
Camera guy fall asleep at the end or something?
Show me the fucking slides, damnit!
here is the link for slides : speakerdeck.com/pyconslides/pythons-class-development-toolkit-by-raymond-hettinger
can't do on a compiled language? swift does it. probably a bit better too.
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?
ty :)
he said that on different platforms math.pi is calculated to different precision. for example on a cray it is 128 bit.
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.
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.
George Clooney of Python!
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'].)
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.
oic! thanks!
Do you need the @radius.setter annotation for that to work?
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.
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
Great talk! I wrote down the code in the talk here www.lvguowei.me/post/python-class-toolkit/ for easy reference.
"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.
who needs more then 6 decimal_digits of pi?
That's not the point.
He's like a way smarter Norm Macdonald
I learned sth new
hah, "consenting adult's language"
He speaks so fast
So am I the only one who struggled to follow because the lecturer talks too fast without pause?
Wrong...you CAN make the instance variables private by prefixing them with two underscores: *self.__radius*.
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.
I learned nothing new from this.
+Flash Man good for you!
_init_. haha. python is a good joke.
Excellent, but stop saying "who learned something new" its tedious
Only theory demonstration. It's a very boring class.