Class Method overload in Python --- mostly, python overload ---

Source: Internet
Author: User
Tags floor division

Class Method overload in Python --- mostly, python overload ---

Reload method format:
Def _ xxx _ (self, other ):
...
Note: Reload method format
----------------------------------------------------------------- Operator
Operator overload:
Purpose:
Let the object created by the custom class perform the import operator operation like the built-in object
Arithmetic Operator:
_ Add _ addition +
_ Sub _ subtraction-
_ Mul _ multiplication *
_ Truedif _ Division/
_ Floordiv _ floor division //
_ Mod _ modulo (remainder) %
_ Pow _ Power **

Reload the inverse Arithmetic Operator:
_ Radd _ (self, lhs) # Add lhs + self
_ Rsub _ (self, lhs) # subtraction lhs + self
_ Rmul _ (self, lhs) # multiplication lhs * self
_ Rtruediv _ (self, lhs) # Division lhs/self
_ Rfloordiv _ (self, lhs) # floor division lhs // self
_ Rmod _ (self, lhs) # modulo lhs % self
_ Rpow _ (self, lhs) # Power Operation lhs ** self
Note: lhs (left hand side) left side

Overload of compound assignment arithmetic operators:
_ Iadd _ (self, other) # add self + = other
_ Isub _ (self, other) # subtract self-= other
_ Imul _ (self, other) # multiplication self * = other
_ Itruediv _ (self, other) # Division self/= other
_ Ifloordiv _ (self, other) # except self // = other
_ Imod _ (self, other) # modulo self % = other
_ Ipow _ (self, other) # Power Operation self ** = other
Note: The overload method is preferred after the overload; otherwise, the _ add _ method is used instead.
------------------------------------------------------------------- Comparison operator
Comparison operator overload:
_ Lt _ less than <
_ Le _ greater than or equal to <=
_ Gt _ greater than>
_ Ge _ greater than or equal to> =
_ Eq _ =
_ Ne _ Not equal! =
------------------------------------------------------------------- Bitwise operation Operator
Bitwise operator overload:
_ And _ bit and &
_ Or _ bit or |
_ Xor _ bitwise xor or ^
_ Lshift _ left shift <
_ Rshift _ right shift>

Bitwise OPERATOR:
_ Rand _ bit and &
_ Ror _ bit or |
_ Rxor _ bitwise XOR or ^
_ Rlshift _ shift left <
_ Rrshift _ right shift>

Compound assignment bitwise operator overload:
_ Iand _ bit and &
_ Ior _ bit or |
_ Ixor _ bitwise XOR or ^
_ Ilshift _ left shift <
_ Irshift _ right shift>
------------------------------------------------------------------- Unary operator
Unary operator overload:
_ Neg _ symbol-
_ Pos _ positive number +
_ Invert _ returns inverse ~
Reload format:
Def _ xxx _ (self ):
Pass
----------------------------------------------------------------- Built-in functions
Built-in function overload:
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 Function
Value Conversion Function overload:
_ Int (obj)
_ Float (obj)
_ Complex (obj)
_ Bool (obj)
------------------------------------------------------------------- Boolean test operator
Boolean test operator overload:
Format:
Def _ bool _ (self ):
....
Purpose:
1) used for bool (obj) function value
2) used in the true value expression of the if statement
3) used in the while statement true value expression
Heavy Load description:
If the _ bool _ (self) method is not available, the true value test will take
The Return Value of the _ len _ (self) method is used to test the Boolean value.
----------------------------------------------------------------- In/not in
In/not in operator overload:
Format:
Def _ contains _ (self, e ):
...
Purpose:
Member qualification test (General)
----------------------------------------------------------------- Index and slice
Overloading of indexing and slicing operators:
Overload method:
_ Getitem _ (self, I) Method
_ Sefitem _ (self, I, v) Method
_ Delitem _ (self, I) Method
Purpose:
Allows you to index and segment custom objects.
Slice overload:
Slice overloading same-sex index overloading public Method
_ Getitem _ (self, I) slice Value
_ Sefitem _ (self, I, v) slice assignment
_ Delitem _ (self, I) del slicing Deletion
------------------------------------------------------------------- Iterator overload
Iterator:
_ Next _ (self ):
Iteratable objects:
_ Iter _ (self ):
------------------------------------------------------------------- With Environment Manager class overload
Classes with the _ enter _ and _ exit _ methods are called environment managers.
The objects that can be managed with must be the Environment Manager.
The _ enter _ method is called to return the object managed by the as variable when the with statement is entered.
The _ exit _ method will be called when you exit the with statement, and you can use parameters to determine whether exceptions occur when you exit the with statement and handle the exceptions accordingly.

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.