When a class is created, it will contain some methods, mainly the following methods:
Proprietary methods of the class :
Method
Description
##__init__
Constructor, call
__del__ when generating the object
Destructor, use
# when releasing the object ##__repr__
Print, convert
__setitem__
Assign value according to index
__getitem__
Get the value according to the index
__len__
Get the length
__cmp__
Comparison operation
__call__
Function call
__add__
Addition operation
__sub__
Subtraction operation
__mul__
Multiplication operation
##__div__
Division operation
__mod__
Remainder operation
__pow__
Power
Of course, sometimes we need to obtain relevant information about the class. We can use the following methods:
type(obj): to obtain the corresponding type of the object;
isinstance(obj, type ): Determine whether the object is an instance of the specified type;
hasattr(obj, attr): Determine whether the object has the specified attributes/methods;
getattr(obj, attr[, default] ) Get the value of the attribute/method. If there is no corresponding attribute, the default value is returned (provided that default is set), otherwise an AttributeError exception will be thrown;
setattr(obj, attr, value): Set the The value of the attribute/method, similar to obj.attr=value;
dir(obj): You can get a list of all attributes and method names of the corresponding object:
2. Method Access control
In fact, we can also regard methods as attributes of the class. Then the access control of methods is the same as attributes, and there is no actual private method. Everything relies on programmers to consciously abide by Python programming standards.
The example is as follows, the specific rules are the same as the attributes,
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning