Never, ever use two leading underscores. This is annoyingly private.
— Ian Bicking Creator of pip, virtualenv, Paste and many other projects
Thanks to the Python data model, your user-defined types can behave as naturally as the built-in types. And this can be accomplished without inheritance, in the spirit of duck typing: you just implement the methods needed for your objects to behave as expected.
In previous chapters, we presented the structure and behavior of many built-in objects. We will now build user-defined classes that behave as real Python objects.
This chapter starts where Chapter 1 ended, by showing how to implement several special methods that are commonly seen in Python objects of many different types.
In this chapter, we will see how to:
format()built-in and the
We’ll do all that as we develop a simple two-dimensional Euclidean vector type.
The evolution of the example will be paused to discuss two conceptual topics: