Python Magic Method ~ ~

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

Python Magic method written by netizens details: details

Magic method Meaning
The basic Magic Method
__new__ (cls[, ...]) 1. __new__ is the first method that is called when an object is instantiated
2. Its first parameter is this class, and the other parameters are used to pass directly to the __init__ method
3. __new__ decides whether to use the __init__ method, because __new__ can invoke the constructor of another class or return the other instance object directly as an instance of this class, __init__ will not be called if the __new__ does not return an instance object
4. __new__ is 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 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 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, instance, owner) Defines the behavior when a descriptor's value is obtained
__set__ (self, instance, value) Defines behavior when the value of a descriptor is changed
__delete__ (self, instance) Defines the behavior when a descriptor's value is deleted
Comparison operators
__lt__ (self, other) Define behavior with 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) Define the equals sign behavior: x = = y call x.__eq__ (y)
__ne__ (self, other) Defines the behavior of an equal sign: X! = y calls x.__ne__ (y)
__gt__ (self, other) Define behavior with 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 operators
__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) Define the behavior of a bitwise OR action: |
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) 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[, modulo]) 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
__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 conversions
__complex__ (self) Define the behavior when called by Complex () (need to return the appropriate value)
__int__ (self) Defines the behavior when 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]) Define the behavior when called by Round () (need to return the appropriate value)
__index__ (self) 1. Implement a reshape cast when an object is applied in a slice expression
2. If you define a custom numeric type that may be used in slicing, you should define the __INDEX__
3. If __index__ is defined, then __int__ also needs to be defined and return the same value
Context management (with statement)
__enter__ (self) 1. Define the initialization behavior when using the WITH statement
2. The return value of the __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. Generally used to deal with exceptions, clear work or do some code blocks after the completion of the daily work
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 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)

Python Magic Method ~ ~

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.