A magic method that is surrounded by double underlines in Python

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

 Basic Magic Method  __new__(cls[, ...]) Used to create objects
1.__new__is the first method that is called when an object is instantiated2. Its first parameter is this class, and the other parameters are used to pass directly to the__init__Method3.__new__Decide whether you want to use the__init__Method, because__new__You can call the constructor method of another class or return the other instance object directly as an instance of this class, if__new__The instance object is not returned, the__init__will not be called4.__new__is primarily used to inherit an immutable type such as a tuple or string__init__(self[, ...]) Initializes the object constructor, which is called when an instance is created__del__(self) destructor, the method that is called when an instance is destroyed__call__(self[, args ...]) Object Plus () call allows an instance of a class to be called like a function: X (A, B) calls X.__call__(A, b)__len__(self) defines the behavior when called by Len ()__repr__(self) defines the behavior when called by Repr ()__str__(self) defines the behavior when called by STR ()__bytes__(self) defines the behavior when called by bytes ()__hash__(self) defines the behavior when a hash () is called__bool__(self) defines the behavior when called by BOOL (), which should return True or False__format__(self, Format_spec) defines the behavior when called by format ()

About Properties __getattr__(self, name) defines the behavior when a user attempts to obtain a property that does not exist__getattribute__(self, name) defines the behavior when the properties of the class are accessed__setattr__(self, name, value) defines the behavior when a property is set__delattr__(self, name) defines the behavior when a property is deleted__dir__(self) defines the behavior of the object when Dir () is called, returns all properties and methods of the key__get__(self, instance, owner) defines the behavior when a descriptor's value is obtained__set__(self, instance, value) defines the behavior when the value of the descriptor is changed__delete__(self, instance) defines the behavior when a descriptor's value is deleted
__dict__ () is a dictionary used to store object properties whose key is the property name and the value of the property.
__mro__ () parse order to see the inheritance order of a class

comparison operators __lt__(self, other) defines the behavior of the less than sign: x < y call x.__lt__(y)__le__(self, other) defines a behavior that is less than or equal to: x <= y calls X.__le__(y)__eq__(self, other) defines the behavior of the equals sign: x = = y calls X.__eq__(y)__ne__(self, other) defines a behavior that is not equal to: X! = y calls X.__ne__(y)__gt__(self, other) defines the behavior of the greater than sign: x > y call x.__gt__(y)__ge__(self, other) defines a behavior greater than or equal to: x >= y calls X.__ge__(y)

Arithmetic Operators __add__(self, other) defines the behavior of addition: +__sub__(self, other) defines the behavior of subtraction:-__mul__(self, other) defines the behavior of multiplication: *__truediv__(self, other) defines the behavior of true division:/__floordiv__(self, other) defines the behavior of integer division://__mod__(self, other) defines the behavior of the modulo algorithm:%__divmod__(self, other) defines the behavior when called by Divmod ()__pow__(self, other[, modulo]) defines when called by power () or * *Behavior when operating__lshift__(self, other) defines the behavior of the bitwise left SHIFT:<<__rshift__(self, other) defines the behavior of the bitwise right SHIFT:>>__and__(self, other) defines the bitwise-and-action behavior of the:&__xor__(self, other) defines the behavior of bitwise XOR or manipulation: ^__or__(self, other) defines the behavior of a bitwise OR operation: |

   Inverse Operation __radd__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rsub__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rmul__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rtruediv__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rfloordiv__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rmod__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rdivmod__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rpow__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rlshift__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rrshift__(self, other) (same as above, called when the left operand does not support the appropriate operation)__rxor__(self, other) (same as above, called when the left operand does not support the appropriate operation)__ror__(self, other) (same as above, called when the left operand does not support the appropriate operation)

   Incremental assignment Operations __iadd__(self, other) defines the behavior of assignment addition: + =__isub__(self, other) defines the behavior of assignment subtraction:-=__imul__(self, other) defines the behavior of the assignment multiplication: *=__itruediv__(self, other) defines the behavior of assignment true division:/=__ifloordiv__(self, other) defines the behavior of an assignment integer division://=__imod__(self, other) defines the behavior of the assignment modulus algorithm:%=__ipow__(self, other[, modulo]) defines the behavior of an assignment power operation: **=__ilshift__(self, other) defines the behavior of the bitwise left shift of the assignment: <<=__irshift__(self, other) defines the behavior of the bitwise right SHIFT of the assignment: >>=__iand__(self, other) defines the behavior of the bitwise AND operation of the assignment: &=__ixor__(self, other) defines the behavior of an assignment bitwise XOR OR operation: ^=__ior__(self, other) defines the behavior of the bitwise OR operation of the assignment: |=

   Unary operator __neg__(self) Defines the behavior of a positive sign: +x__pos__(self) Defines the behavior of the minus sign:-x__abs__(self) defines the behavior when called by ABS ()__invert__(self) Defines the behavior of bitwise negation: ~x

   Type Conversions __complex__(self) defines the behavior when called by Complex () (requires returning the appropriate value)__int__(self) defines the behavior when being called by INT () (requires returning the appropriate value)__float__(self) defines the behavior when a float () is called (the appropriate value needs to be returned)__round__(self[, n]) defines the behavior when called by Round () (requires returning the appropriate value)__index__(self)
1when an object is applied in a slice expression, the reshape cast is implemented2. If you define a custom numeric type that may be used in slicing, you should define__index__3. If__index__is defined, the__int__Also need to be defined and return the same value

   Context Management (with statement) __enter__(self)
1. Define the initialization behavior when using the WITH statement2.__enter__The return value is bound by the target of the WITH statement or the name after__exit__(Self, Exc_type, Exc_value, Traceback)
1. Defines what the context manager should do when a block of code is executed or terminated2typically used to handle exceptions, clear work, or do routine work after a block of code has been executed

  Container type __len__(self) defines the behavior when called by Len () (returns the number of elements in the container)__getitem__(self, key) defines the behavior that gets the specified element in the container, equivalent to Self[key]__setitem__(Self, key, value) defines the behavior of the specified element in the set container, equivalent to self[key] =value__delitem__(self, key) defines the behavior of the specified element in the Delete container, equivalent todelSelf[key]__iter__(self) defines the behavior of elements in an iteration container__reversed__(self) defines the behavior when called by reversed ()__contains__(self, item) defines when the member test operator is used (inchOr not inch) When the behavior

A magic method that is surrounded by double underlines in Python

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.