Python has many built in magic functions that are used internally by classes for certain actions. For example, adding two numbers calls the
__add__ method and iterating over a list calls the
__iter__ method. I will expand on this Duck Typing principle and enumerate over a range of magic methods that you can add to your classes to have a cleaner codebase.
In this talk, I will introduce and enumerate over magic methods available in Python. This is an introductory talk for anyone with basic familiarity of Python. For each class of magic methods that I introduce, I'll provide example code samples.
I will start off by introducing basic magic methods that allow you to do things like initializing objects and printing readable versions of objects. I will then go over select magic methods that allow for emulating numeric types. I will then cover methods that enable you to emulate sequences and write objects that can be indexed and iterated over. I will conclude by talking about context managers (that allow for managing pre-step and post-step actions) and Abstract Base Classes in the abc module that will allow for you to get free functionality if you provide the implementation for certain magic methods on your classes.