Skip to main content

A Hundred Days of Code, Day 016 - Python, Advanced Objects.

Started up with Python Objects today.
This seems like a long, hard one.
Here’s hoping I learn lots.
Knowing Reuven, I know I will.

Notes

Part 1, Advanced Methods.

This section focuses on the dunder methods in Python.

  • By default objects are not equal to other objects even they they are from the same class, with the same attributes and methods.

    • We could implement our own equals method though.
    • The point being, we need to be intentional and methodical, when we design and implement our classses
  • Static methods are plain old functions that I write in my class for use by both the class and the instance. Could be used to clean up input or similar such utility functions. I create them by putting a @staticmethod decorator atop my method.

  • Class methods are those that are meant for use by only the class. I can call them from my instances, but they act on the class. I create them by using the @classmethod atop my method. For clarity, the first variable is now written as cls instead of self.

Part 2, Inheritance.

  • MRO, Method Resolution Order. I can do an classname.__mro__ to see how Python goes about looking for methods upwards, specially if I’m inheriting from multiple objects. Running help on an object also lists the MRO of its methods.
  • Best to explicity inherit from all parent classes, when inheriting from multiple classes.
  • We can use the classname.__bases__ to check for the classes parents.
  • A mixin is a class, we can inherit from (probably in addition to and before our parent class), that just adds functionality to our class. Does nothing sematically / organizationally to our objects or classes.
    • The mixin must come first.
  • Adding an __ before an attribute, signifies that it is not to be touched. A little security by obscurity too. Nothing’s stopping me from actually going and changing things up. It’s just a notifier to people with common sense that such stuff ought not to be tampered with.
    • The main reason though is to prevent conflict between attribute names.

Learning / Feedback/ Experiences from doing exercises

  • This course feels like the O’Reilly cookbooks in video form.
  • The first section takes all the operators, I take for granted in Python and then shows me how to implement them with my own classes
    • how do I get len to work on my object? or how do I implement the == operator in my classes? etc. etc.
  • Starting with the last one and this one, have given up on exercises and just taking notes for now. As soon as I finish the run, I will be then going on an exercise binge with these pending exercises as well as Reuven’s Python Workout.
  • The execption being, if I run into a topic that is really confusing, then I will attempt the exercises to drill the idea immediately into my head.
  • Obligatory Reuven plug, since I started up a new course :)
    • Reuven is an awesome teacher.
    • He takes the time, to explain stuff in painstaking detail, leaving nothing as an “exercise to the reader”.
    • All of these courses, so far have been worth every single penny I spent on them.

Read all about my Python Objects journey here