Python Special Methods-Special method

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators




Special methods

Special methods exist to be called to the Python interpreter, and usually do not need to call them directly.
This means that my_object.__len__ () should not be used, but should be called with Len (My_object).
When executing len (my_object), if My_object is an object of a custom class, the Python interpreter
To invoke the __len__ method of the owning class.

However, if My_object is a Python built-in type, such as List, str, ByteArray, ETC.,
Then the Python interpreter CPython will take a shortcut, and the __len__ method will actually return the Ob_size attribute directly 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.
Usually the invocation of a special method is implicit, such as for I in X: This statement is actually called the ITER (x) function,
And behind this function is the x.__iter__ () method. The premise, of course, is that this method is implemented in X.

Usually the code does not need to use special methods directly. An exception is the __init__ method, which requires that the constructor of the superclass (__init__) be called in the __init__ method of the subclass in the code.
For example, super (Basecls, self). __init__ (args).

Using special methods with built-in functions (such as Len, ITER, str, and so on) is the best choice. These built-in functions not only invoke special methods, they are usually faster.


List of special methods
Category Method name
string/byte sequence representation __repr__, __str__, __format__, __bytes__
numeric conversions __abs__, __bool__, __complex__, __int__, __float__, __hash__, __index__
Set simulation __len__, __getitem__, __setitem__, __delitem__, __contains__
Iterative enumeration __iter__, __reversed__, __next__
Can call __call__
Context Management __ENTER__, __exit__
Creating and destroying instances __new__, __init__, __del__
Property Management __getattr__, __getattribute__, __setattr__, __delattr__, __dir__
Property descriptor __get__, __set__, __delete__
Services related to the class __prepare__, __instancecheck__, __subclasscheck__
Unary operators __neg__-, __pos__ +, __abs__ abs ()
Many comparison operators __lt__ <, __le__ <=, __eq__ = =, __ne__! =, __gt__ >, __ge__ >=
Arithmetic operators

__add__ +, __sub__-, __mul__ *, __truediv__/, __floordiv__//, __mod__%, __divmod__ divmod (), __pow__ * * or POW (), __roun D__ round ()

Inverse arithmetic operator __radd__, __rsub__, __rmul__, __rtruediv__, __rfloordiv__, __rmod__, __rdivmod__, __rpow__
Incremental assignment arithmetic operators __iadd__, __isub__, __imul__, __itruediv__, __ifloordiv__, __imod__, __ipow__
Bitwise operators __invert__ ~, __lshift__ <<, __rshift__ >>, __and__ &, __or__ |, __xor__ ^
Inverse bitwise operators __rlshift__, __rrshift__, __rand__, __rxor__, __ror__
Increment Assignment bitwise operator __ilshift__, __irshift__, __iand__, __ixor__, __ior__


Python Special Methods-Special method

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.