The first chapter summarizes the data model

By implementing special methods, custom data types can act like a built-in types, such

Let us write more expressive code - or, more Python-style code.

A basic requirement for Python object is that it must have a reasonable representation of the string, we can meet this requirement by __repr__ and __str__. The former help us debug and logging, the latter is to see to the end user . That is why there is a special method __repr__ and __str__ data model.

Simulation of the sequence data is a special type of method used most places, it has been demonstrated in the example FrenchDeck class.

In Chapter 2, we will highlight sequence data type, and then in Chapter 10, we'll Vector class expanded into a multi-dimensional data types, through this exercise, you will have the opportunity to achieve self-defined sequence.

Python provides a rich value type operator overloading by this model, in addition to those built, and also decimal.Decimal fractions.Fraction. These data types are supported infix arithmetic operators. In Chapter 13, we will extend through the Vector class to learn how to implement these operators, of course, also mentioned how operators commutative and enhanced assignment.

Python special method data model, there are many, the book will cover the vast majority of them, to explore how to use and implement them.

Guess you like

Origin www.cnblogs.com/yuchen991/p/12441533.html