Most of the class method overloads in Python---

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise

Overloaded method Format:
def __xxx__ (Self,other):
...
Note: Overloaded method format
-----------------------------------------------------------------operator
Operator Overloading:
Role:
Let custom classes create objects like built-in objects input operator operations
Arithmetic operators:
__add__ addition +
__sub__ Subtraction-
__mul__ multiplication *
__TRUEDIF__ Division/
__FLOORDIV__ floor except//
__mod__ modulus (redundancy)%
__POW__ Power * *

Inverse arithmetic operator overloading:
__radd__ (self, LHS) # addition LHS + self
__rsub__ (self, LHS) # subtraction LHS + self
__rmul__ (self, LHS) # multiplication LHS * Self
__rtruediv__ (self, LHS) # Division Lhs/self
__rfloordiv__ (self, LHS) # floor except LHS//Self
__rmod__ (self, LHS) # modulo lhs% self
__rpow__ (self, LHS) # Power Operation LHS * * Self
Note: LHS (left hand side)

Overloading of compound assignment arithmetic operators:
__iadd__ (self, Other) # addition self + + other
__isub__ (self, other) # subtraction self-= other
__imul__ (self, other) # multiplication self *= Other
__itruediv__ (self, Other) # Division self/= Other
__ifloordiv__ (self, other) # floor except self//= other
__imod__ (self, other) # modulo self%= Other
__ipow__ (self, Other) # Power Operation Self **= Other
Note: When overloading is the preferred method, use __add__ and other methods instead
-----------------------------------------------------------------comparison Operators
comparison operator Overloading:
__lt__ less than <
__le__ greater than or equal to <=
__gt__ greater than >
__ge__ greater than or equal to >=
__eq__ equals = =
__ne__ Not equal to! =
------------------------------------------------------------------bit manipulation operators
Bitwise manipulation Operator Overloading:
__and__ bit and &
__or__ bit or |
__xor__ bit xor ^
__lshift__ left Shift <<
__rshift__ Right Shift >>

Inverse bitwise manipulation Operators:
__rand__ bit and &
__ror__ bit or |
__rxor__ bit xor ^
__rlshift__ left Shift <<
__rrshift__ Right Shift >>

Compound Assignment bitwise operator Overloading:
__iand__ bit and &
__ior__ bit or |
__ixor__ bit xor ^
__ilshift__ left Shift <<
__irshift__ Right Shift >>
-----------------------------------------------------------------unary operators
Overloading of unary operators:
__neg__ symbol-
__pos__ Plus +
__INVERT__ Counter ~
Overloaded format:
def __xxx__ (self):
Pass
-----------------------------------------------------------------built-in functions
Built-in function overloading:
def __abs__ (self) ABS (obj) function call
def __len__ (self) len (obj) function call
def __reversed__ (self) reversed (obj) function call
def __round__ (self) round (obj) function call
-----------------------------------------------------------------Numeric Conversion functions
numeric conversion function Overloading:
__int__int (obj)
__float__float (obj)
__complex__complex (obj)
__bool__bool (obj)
-----------------------------------------------------------------Boolean test operators
Boolean Test operator overloading:
Format:
def __bool__ (self):
....
Role:
1) for bool (obj) function values
2) used in the If Statement truth expression
3) for the while Statement truth expression
Overload Description:
When there is no __bool__ (self) method, the truth test takes
The return value of the __len__ (self) method to test the Boolean value
-----------------------------------------------------------------In/not in
In/not in operator overloading:
Format:
def __contains__ (self, E):
...
Role:
Membership testing (usually)
-----------------------------------------------------------------indexes and slices
Overloading of index and tile operators:
Overloaded methods:
__getitem__ (self, I) method
__sefitem__ (self, I, V) method
__delitem__ (self, I) method
Role:
Allow objects of a custom type to be indexed and sliced
Slice (slice) overloading:
Section overloading common methods for same-sex index overloading
__getitem__ (self, i) slice value
__sefitem__ (self, I, v) slice assignment
__delitem__ (self, i) del slice delete
-----------------------------------------------------------------iterator overloading
Iterators:
__next__ (self):
Objects that can be iterated:
__iter__ (self):
-----------------------------------------------------------------with Environment Manager in-class overloading
Classes with __enter__ and __exit__ methods within a class are called environment managers
The object that can be managed with a must be an environment manager
The __enter__ method is called when it enters the WITH statement to return an object managed by the as variable
The __exit__ method is called when it leaves the With statement, and parameters can be used to determine if an exception occurred while leaving the with statement and to handle it accordingly

Most of the class method overloads in Python---

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.