Fluent Python: The power of special methods
You’ve used Python for years. Do you really know it? Be brave and skin that Python. In this cut, Luciano Ramalho probes deep into special methods and the Data Model. Become fluent with idiomatic Python.
Pythonistas praise a good API by calling it “Pythonic.” That quality has much to do with proper use of the special methods used in the Python Data model, which define the essential behaviors that we expect in objects.
Perhaps you’ve used Python for years. Do you really know it? This tutorial is intended for a Python programmer who has working/practical knowledge of the language plus an understanding of object-oriented programming, who now needs to learn how to write idiomatic APIs. While it’s easy to get started with Python, mastering the language may take a long time. Once you understand special methods and the Python data model, you get a deeper understanding of the language. Luciano Ramalho shows how in this tutorial.
Here’s a clip:
If you learned another object-oriented language before Python, you may have found it strange to call
len(collection)
instead ofcollection.len()
. This apparent oddity is the tip of an iceberg that, when properly explored, is the key to everything we call Pythonic. The iceberg is called the Python data model, and it describes the API that you can use to make your own objects play well with the most idiomatic language features.