Summary of magical functions in Python

Source: Internet
Author: User
Tags bitwise integer division

Basic Magic MethodFunction __new__(cls[, ...])1. NewIs the first method 2 that is called when an object is instantiated. Its first parameter is this class, and the other parameters are used to pass directly to the InitMethod 3. NewDecide whether you want to use the InitMethod, because NewYou can call the constructor method of another class or return the other instance object directly as an instance of this class, if NewThe instance object is not returned, the InitWill not be called Newis primarily used to inherit an immutable type such as a tuple or string __init__(self[, ...])constructor, the initialization method that is called when an instance is created __del__(self)destructor, the method that is called when an instance is destroyed __call__(self[, args...])Allows an instance of a class to be called as a function: X (A, B) calls X. Pager(A, B) __len__(self)Defines the behavior when called by Len () __repr__(self)Defines the behavior when called by Repr () __bytes__(self)Defines the behavior when called by bytes () __str__(self)Defines the behavior when called by STR () __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 ()
attribute-related magic methods function
__getattr__(self, name) Defines the behavior when a user attempts to obtain a nonexistent property
__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 when Dir () is called
__get__(self, instence, owner) Defines the behavior when a descriptor's value is obtained
__set__(self, instence, value) Defines behavior when the value of a descriptor is changed
__delete__(self, instence) Defines the behavior when a descriptor's value is deleted
comparison operator Magic method function
__lt__(self, other) Define less than sign behavior: 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) Define the equals sign behavior: x = = y call x.eq(y)
__ne__(self, other) Define a behavior that is not equal to: X! = y calls x.ne(y)
__gt__(self, other) Define behavior for greater than sign: x > Y call x.gt(y)
__ge__(self, other) Define a behavior greater than or equal to: x >= y calls x.ge(y)
Arithmetic
Operation Magic Method function
__add__(self, other) Define the behavior of the addition: +
__sub__(self, other) Define the behavior of subtraction:-
__mul__(self, other) Define 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) Define the behavior of the modulo algorithm:%
__divmod__(self, other) Defines the behavior when called by Divmod ()
__pow__(self, other[, modulo]) Defines the behavior when the power () call or the * * operation
__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:&
__xor__(self, other) Define the behavior of bitwise XOR or manipulation: ^
__or__(self, other) Defines the behavior of a bitwise OR operation: \
Anti-Operation Magic Method function
__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 Operation Magic Method function
__iadd__(self, other) Define the behavior of the assignment addition: + =
__isub__(self, other) Define the behavior of assignment subtraction:-=
__imul__(self, other) Define 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) Define the behavior of the assignment modulus algorithm:%=
__ipow__(self, other) Defines the behavior of an assignment power operation: **=
__ilshift__(self, other) Define assignment bitwise Left SHIFT behavior: <<=
__irshift__(self, other) Define assignment bitwise Right SHIFT behavior: >>=
__iand__(self, other) Define assignment bitwise-and-action behavior: &=
__ixor__(self, other) Defines the behavior of assignment bitwise XOR or manipulation: ^=
__ior__(self, other) Defines the behavior of the bitwise OR operation of an assignment: |=
Unary
operator Magic method function
__neg__(self) Define the behavior of a positive sign: +x
__pos__(self) Define the behavior of the minus sign:-X
__abs__(self) Define behavior when called by ABS ()
__invert__(self) Define the bitwise negation behavior: ~x
Type Conversion Magic method function
__complex__ (self)
__int__ (self) Define the behavior when being called by INT () (need to return the appropriate value)
__float__ (self) Define the behavior when the float () is called (need to return the appropriate value)
__round__ (self[, n]) Defines the behavior when called by the Round () (need to return the appropriate value)
__index__ (self) index 3. If index is defined, int also needs to be defined, and the same value is returned
Contextual management (with statements) related magic methods function
__enter__(self) 1. Define the initialization behavior when using the WITH statement 2. The return value of enter is bound by the target of the WITH statement or the name after as
__exit__(self, exc_type, exc_value, traceback) 1. Define what the context manager should do when a block of code is executed or terminated 2. Typically used to handle exceptions, clear work, or do daily work after a block of code has been executed
container-related magic methods function
__len__(self) Defines the behavior when called by Len () (returns the number of elements in the container)
__getitiem__(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 settings container, equivalent to self[key] = value
__delitem__(self, key) Defines the behavior of the specified element in the delete container, which is equivalent to Del Self[key]
__iter__(self) Defines the behavior of elements in an iteration container
__reversed__(self) Defines the behavior when called by reversed ()
__contains__(self, item) Define behavior when using the member test operator (in or not)

Summary of magical functions 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.