A Guide to Python’s Magic Methods

Categories:

Recommended

Introduction

This guide is the culmination of a few months’ worths of blog posts. The subject is magic methods.

What are magic methods? They’re everything in object-oriented Python. They’re special methods that you can define to add ”magic” to your classes. They’re always surrounded by double underscores (e.g. __init__ or __lt__). They’re also not as well documented as they need to be. All of the magic methods for Python appear in the same section in the Python docs, but they’re scattered about and only loosely organized. There’s hardly an example to be found in that section (and that may very well be by design, since they’re all detailed in the language reference, along with boring syntax descriptions, etc.).

So, to fix what I perceived as a flaw in Python’s documentation, I set out to provide some more plain English, example-driven documentation for Python’s magic methods. I started out with weekly blog posts, and now that I’ve finished with those, I’ve put together this guide.

I hope you enjoy it. Use it as a tutorial, a refresher, or a reference; it’s just intended to be a user-friendly guide to Python’s magic methods.

 

Category:

Attribution

Rafe Kettler. A Guide to Python’s Magic Methods. https://github.com/RafeKettler/magicmethods

VP Flipbook Maker

Convert your work to digital flipbook with VP Online Flipbook Maker! You can also create a new one with the tool. Try it now!