Part IV the 9th chapter, the Python style object
This chapter is followed by the 1th chapter, which illustrates the common method of special implementation.
This chapter covers the topic:
- Supports built-in functions for generating other representations of objects (such as repr (), bytes (), and so on)
- Implementing alternative construction methods using a class method
- Extend the format of the built-in formats () and Str.format () to micro-language
- Implement read-only properties
- Turns an object into a hash so that it is used in the collection and as a Dict key
- Save memory with __slots__
- How and when to use the @classmethod and @staticmethod adorners
- Usage, conventions, and limitations of private and protected properties for Python
A simple two-dimensional Euclidean vector type will be developed, covering all of the above topics in this process.
Mimics the behavior of the Python standard object, allowing the vector to behave in Python style.
1. Object representation
Object-oriented languages have at least one standard form of getting the string representation of an object. Python is available in two ways.
1.1 repr (): Returns the string representation of an object in a way that is easy for developers to understand .
1.2 str (): Returns the string representation of an object in a way that is easy for the user to understand .
1.3 Implements __REPR__ and __str__ special methods to provide support for repr () and STR ().
1.4 repr,str, and __format__ all return Unicode strings (str type). Only the __bytes__ method should return a sequence of bytes (bytes type).
Leave the pit, it's too complicated.
Chinese e-book P387
Fluent_python_part4 object-oriented, 09-pythonic-obj,python-style objects