Done with Reuven Lerner’s OOP basics
lendoes the right thing across multiple types of objects.
- it counts characters in a string, elements in a list, and key-value pairs in a dictionary. how does it know how to do that?
- that is because
lenuses a magic method (methods that have a
__for a prefix and suffix)
- len uses the
__len__method which i can use in my own classes, to implement a
lenmethod for my classes
- len uses the
- If I choose to do so, I need to realise what those underlying methods expect to return and return the same type in my classes.
__len__expects to return integers, so when I implement it in my classes, I ought not to return a string. Once again Reuven strongly suggests using common sense :)
- same thing when i try to print something. it calls the magic method
__str__to do stuff. or if i try to do repr to check for raw representations, the
__repr__method is called.
- Python is filled with these methods which I can use to allow my classes to have standard Python like functionality. like a
print(doggie)should give me details about the cute pup object i just created, if I have taken the time and attention to implement the
- most of these methods come with the base primitive class
objectwhich every class inherits from. so i get them for free. and then i can override them and customise them to what I want to show.
- people try standard methods on new objects. implementing them is much better than creating my own and asking people to use ’em.
- if both are equal or really similar, ok to just use only
__repr__. later when I have experience and other needs, i can implement
learning / feedback/ experiences from doing exercises
- Despite warning myself above, I went ahead and made the mistake of returning a whole lovely string, instead of an integer, when I tried to implement the
lenmethod on my class XD
- and then I forgot to add the f prefix to an f-string and wondered why it was not interpolating for nearly 30 mins
- Reuven has a wry sense of humour. In one of my exercises I get to create classes of animals for a zoo, with various attributes such as colour and legs and so forth. Then he has me put them in cages. After making me put 2 sheep in a cage with a wolf, he wonders if the legs of the sheep ought to be reduced or not XD
- The number of typos I am making is staggering. Missing colons, missing quotes, missing brackets. If this is Python, I shudder to think how I’d fare if I picked up a static language to learn XD
- Wasted another half an hour because I spelt colours with capitals like Black and then later looked for black and was tearing my hair out, because I knew there were black animals and why in tarnation, would Python not show me them XD
- Saw Reuven do nested comprehensions and now I want to do that too! it took him one well thought out line to do what I did in 2 multiline functions
This was amazing!
I learnt so much about classes and the way they work.
Reuven has a very joyful pedagogy.
I love him go hahahaha like Santa Claus.
I love the fact that he makes mistakes.
I love that for every problem, he poses, he has various approaches.
His fluency shines through every lesson.
I got lucky with the Pycharm Humble Bundle.
But I loved this so much, that I am going to sign up for all the Lerner courses, I possibly can.
Beginning with the basics.
If OOP is any indicator, the journey is going to be lots of fun.