Fluent python the first chapter of the Python Data Model learning record

Source: Internet
Author: User

There are special methods in Python that start with a double underscore and end with a double underline. As __getitem__, these methods are special methods that are used internally by the Python interpretation authority and generally do not need to be called

There is also a double underscore, such as __xxx, these methods are private methods for the internal use of the class, it is best not to use outside.

How to use special methods

MY_OBJECT.__LEN__ () This is for internal use, you should use Len (my_object) for external use, and if My_object is a custom class, Python calls its internal __len__ method, and of course we can override that method

If it is a python built-in type, such as a list, a string, a sequence of bytes, and so on, then CPython will take a shortcut, and __len__ will actually return directly to the Ob_size attribute in Pyvarobject. Pyvarobject is a C-language struct that represents a variable-length built-in object in memory. Reading this value directly is much faster than calling a method.

Most of the time, the invocation of a particular method is implicit, such as for the I in X: This statement, behind which the utility is ITER (x), and behind this function is the x.__iter__ () method. Of course, the assumption is that the method is implemented in X.

There is usually no need to use special methods directly in the code, unless there is a lot of meta-programming, the frequency of calling special methods should be much lower than the number of times you have to implement them. The only exception may be the __init__ method. It is often used in code. The goal is to call the superclass's constructor in the __init__ method in the subclass

String representation

Python has a built-in function called repr, which can be used to express an object in the form of a string for identification, which is called "string representation", repr is to get a string representation of an object by __repr__ this particular method, if the __repr__ method is not implemented. When we print an instance of an object in the console, we get the string that <xxx object at 0x10e100070>,__repr__ should be accurate, unambiguous, and show as much as possible how to create the printed object in code.

The difference between __repr__,__str__ is that the latter is used in the STR () function or is called in the print function, and the returned string is more friendly to the end user.

If you only want to implement one of these two special methods, __repr__ is a better choice, if an object does not have a __str__ function, and Python needs to call it, the interpreter uses __repr__ instead

A custom Boolean value

By default, instances of our own defined classes are always considered true unless the class has its own implementation for __bool__ or __LEN__ functions. The result of x.__bool__ () is always invoked behind BOOL (x), and if there is no __bool__ method, BOOL (x) attempts to invoke x.__len__ (). Returns 0 if BOOL returns false, otherwise true,

Special methods at a glance

Fluent python the first chapter of the Python Data Model learning record

Related Article

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.