| Method |
Description |
Remarks |
| 1. Basic Methods |
| _ New _ (cls [, * argv]) |
1. _ new _ is the first method called when an object is instantiated. 2. Its first parameter is this class. Other parameters are directly passed to the _ init _ method. 3. _ new _ determines whether to use the _ init _ method, because _ new _ can call the constructor of other classes or directly return other instance objects as instances of this class. If _ new _ does not return instance objects, then _ init _ will not be called 4. _ new _ is mainly used to inherit an unchangeable type, such as a tuple or string |
Cls: indicates the name of a class. Self: The name of an instance object. |
| _ Init _ (self [, * argv]) |
Constructor called when an instance object is defined |
Constructor similar to C ++ |
| _ Del _ (self) |
The Destructor called when an instance object is deleted. |
Destructor similar to C ++ |
| _ Call _ (self [, * argv]) |
Allows a class to be called like a function.
|
Class_x (a, B) actually calls class_x. _ call _ (a, B)
|
| _ Len _ (self) |
Obtains the length of an instance object.
|
Same result as calling function len (obj)
|
| _ Repr _ (self) |
Converts an instance object to a string. |
For example, if ls = [1, 2, 3], the repr (ls) is '[1, 2, 3]', which is the same as the repr (obj) function. |
| _ Str _ (self) |
Converts an instance object to a string. |
The difference with repr () is that the str (obj) string is printed to make people look more friendly, while repr (obj) is for the interpreter; If a = xxx (list, Dictionary, ancestor, or set)
Eval (repr (a) = a was established Eval (str (a) = a is not necessarily true |
| _ Int _ (self) |
Defines the behavior when int () is called. |
|
| _ Float _ (self) |
Define the behavior when called by float () |
|
| _ Round _ (self [, n]) |
Behavior when called by round () |
Round (digit [, n]) retains n-bit precision of digit numbers |
| _ Hash _ (self) |
Defines the behavior that can be called by hash ().
|
|
| _ Bytes _ (self) |
Define the behavior called by bytes () |
|
| _ Bool _ (self) |
Define the behavior called by bool ()
|
Returns True (1) or False (0)
|
_ Format _ (self, form)
|
Defines the behavior called by format ().
|
|
| 2, Operator Method |
_ Add _ (self, other)
|
Addition: +
|
|
_ Sub _ (self, other)
|
Subtraction :- |
|
_ Mul _ (self, other)
|
Multiplication :*
|
|
| _ Truediv (self, other) |
Division :/ |
Note: truediv
|
| _ Floordiv (self, other) |
Integer Division :// |
Floor () is used to take down an integer. |
_ Mod _ (self, other)
|
Remainder: % |
|
| _ Pow _ (self, other [, mod]) |
Multiplication party :**
|
Pow (x, y [, z]), If no Z exists, return x ** y
If Z exists, return x ** y % z |
| _ Divmod _ (self, other) |
Divmode ()
|
The returned value is the ancestor (Business Value, remainder)
|
_ Lshift _ (self, other)
|
Left shift: < |
|
| _ Rshift _ (self, other) |
Shift right:>
|
|
| _ And _ (self, other) |
Bitwise AND :& |
Note that the following operations are performed by bit, not by logic.
|
| _ Or _ (self, other) |
By bit or: |
|
|
| _ Xor _ (self, other) |
Bitwise OR: ^ |
|
| 3, Anti-Operator Method |
_ Radd _ (self, other)
|
Addition, such as a + B. This function is called when a does not support the _ add _ () operation;
|
Add 'R' to the operator.
|
| _ Rsub _ (self, other) |
Other-self
|
|
| ............ |
|
|
| 4, Incremental value assignment operator method |
_ Iadd _ (self, other)
|
Value assignment addition: + =
|
Add 'I' before the value assignment operator.
|
_ Isub _ (self, other)
|
Value assignment subtraction:-=
|
Self = self-other
|
............
|
|
|
| 5, Unary operator method |
| _ Pos _ (self) |
Define the positive number: + x |
|
| _ Neg _ (self) |
Defines a negative number:-x |
|
| _ Abs _ (self) |
Take absolute value |
|
| _ Invert _ (self) |
Reverse by bit :~ X |
|
| 6, Comparison operator method |
| _ Gt _ (self, other) |
Greater than:> |
|
| _ Ge _ (self, other) |
Greater than or equal to:> = |
|
| _ Lt _ (self, other) |
Less than: < |
|
| _ Le _ (self, other) |
Less than or equal to: <= |
|
| _ Eq _ (self, other) |
Equal: = |
|
| _ Ne _ (self, other) |
Not supported :! = |
|
| 7, Attribute operations |
| _ Getattr _ (self, name) |
Called when a user accesses a non-existent attribute |
Note that object/super () (the base class of all classes) does not have this method. |
| _ Getattribute (self, name) |
Called when accessing an existing property |
Call this function first. If this property cannot be found, call the above property. |
| _ Setattr _ (self, name, value) |
Called when setting properties |
|
| _ Delattr _ (self, name) |
Called When deleting an attribute |
|
| Property (fget = None, fset = None, fdel = None, doc = None) |
Is a class, the main function is to facilitate the call of class internal functions |
1 class C(object): 2 def getx(self): return self._x 3 def setx(self, value): self._x = value 4 def delx(self): del self._x 5 x = property(getx, setx, delx, "I'm the 'x' property.") 6 7 8 >>> c=C() 9 >>> c.x=1010 >>> c.x11 10 |
| _ Get _ (self, instance, owner) |
Called when descriptor is accessed |
For more information, click here |
| _ Set _ (self, instance, value) |
Called when the descriptor is changed |
|
_ Delelte _ (self, instance, value)
|
Called when the descriptor is deleted |
|
| 8, Container type operation |
| _ Getitem _ (self, key) |
Obtains the behavior of a specified element in a container. |
|
| _ Setitem _ (self, key, value) |
Set the behavior of the specified Element in the container |
|
| _ Delitem _ (self, key) |
Deletes a specified element in a container. |
|
| _ Iter _ (self) |
Define the behavior of elements in the iterator |
|
| _ Reversed _ (self) |
When calling the reversed () function |
|
| _ Contains _ (self, item) |
Behavior of the member operator in/not in |
|
|
PS:①. For all the above magic methods, use the _ xx _ form (_ is double "_", Double underline)
②. The above magic method is automatically called by the Python interpreter, and can also be manually called.
③ Magic method the Python interpreter automatically provides the default value. Therefore, unless you need to change its internal function, use the default magic method at other moments.
4. The magic method is for the class. It makes no sense to talk about magic_method without the "class ".
⑤. * Argv is a variable parameter list, similar to the va (variable argument) in C language. Note the difference with the pointer. in python, the pointer is temporarily forgotten because the memory mechanism of python is automatically completed by the interpreter.
|