Fluent_python,01-data-model

Source: Internet
Author: User

01-data-model/frenchdeck.py1. When the Python interpreter encounters a special syntax, it uses the special method __ to activate some basic object operations.

The existence of special methods is for the purpose of being interpreted. Without My_object.len (), Len (my_object) should be used.
such as My_collection[key], my_coolection._getitem_(key).
For example, if you implement __len () __ in class Frenchdeck, you can use Len (object).
Implement _GetItem () _ You can use Deck[position].

2. Many times the invocation of the special method __ is implicit

For example, for I in x:-X._iter_(), if __iter__ is implemented in X.

3. Python contains a function random.choice that randomly selects an element from the sequence.
    fromimport choice    = [1,2,3,4,5]    choice(a)
4. Because class Frenchdeck is an iterative __, the __in operator __ can be used on the Frenchdeck class. In addition, if a collection type does not implement the __contains__ method, the in operator does an iterative search sequentially.
>>> Card(‘Q‘‘hearts‘in deckTrue>>> Card(‘Q‘‘beasts‘in deckFalse
01-data-model/vector2d.py5. Use the Special method __abs__ to implement the vector, __add__ implement the ' + ' operator, __mul__ implement ' * ' operator 6. After implementing __repr__

V1 = vector ()
Both print (v1) and repr (v1) can express an object in the form of a string.

7.%s differs from%r,%r output is raw data

%s with Str (). %r use Repr ().
%s to the user,%r for debug.
__REPR__ goal is to be unambiguous
__STR__ goal is to be readable
If an object does not have a __str__ function, the Python interpreter uses __repr__ as an alternative.

fromimport= datetime.today()print(‘%s‘% d)print(‘%r‘% d)
8. BOOL (x) is X._ BOOL (), if no _bool exists() calls __len__ () 9. Special methods List Chinese e-book P57 Summary:

Academic: Python prefers to say data types, which in fact means object models. By implementing a special method of Python data types to provide a rich set of meta-object protocols (the meta-object is the core object for the build language, the protocol means the API to build the core language), the custom type can behave like the built-in content, thus writing more flexible and Pythonic code.

Popularization: By implementing a Special method (_method_), some behaviors and functions of the object can be given.

Fluent_python,01-data-model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.