Today's interim summary of the content, and some may not be the right place to make a cheat sheet when it initially.
Class of built-in functions (inherited object of their own rewrite)
Built-in functions | Execution timing | important point | Call the Case |
---|---|---|---|
__init__ |
When an object is instantiated | Not allowed to write the return value (return None and did not return difference) subclass overrides a __init__() method in a subclass to __init__() call the ancestor method __init__ method ( super(当前类, self).__init__(参数) ) |
stu = Student() |
__new__ |
It is called the class instantiated | stu = Studetn() |
|
__call__ |
When the calling object | Remember to call the parent class | stu(__call__方法的参数) |
__str__ |
When the object into a string | Must return the return value of type string (the default value type + return address, lacks significance) | print(stu) |
__del__ |
When the object is removed (manual or program ends, garbage collection) | del stu |
|
__getattr__ (Method Principle) |
Use when accessing properties | The property does not exist before the execution, there is __getattribute__ a method will first execution __getattribute__ method |
stu.gaewfg |
__setattr__ (Method Principle) |
When executed with. Add / Modify Properties | stu.sex = 'male' |
|
__delattr__ (Method Principle) |
When delete an attribute property, execution del objects | Attribute is deleted when execution | del stu.age |
__getattribute__ |
Use when accessing properties | Regardless of whether the property will not have to perform, I wrote this method after__getattr__需要自行调用 |
stu.age |
__getitem__ ([] Principle) |
When using [property name] property access | stu[age] |
|
__setitem__ ([] Principle) |
When editing or adding attributes with [attribute name] = | stu[age] = 18 |
|
__delitem__ ([] Principle) |
When you delete a property with a [property name] | del stu[age] |
|
__gt__ __ge__ __eq__ __ne__ __lt__ __le__ Comparison operator (comparison of custom rules) |
When calling the corresponding comparison operators | Like __gt__ 和 __lt__ this can only specify a |
stu1 > stu2 |
__iter__ _next__ Iterator protocol |
When the value of the object as an iterative | for i in stu: ... |
|
__enter__ __exit__ Context Management |
With at the time with the back | with MyOpen(__enter__ 的参数) as m: |
Built-in property class (can directly call)
Attributes | effect | important point | Case |
---|---|---|---|
__dict__ |
Returned to the caller's own namespace | stu.__dict__ |
|
__class__ |
Returns the current calling object class | ||
__slots__ |
Specified class object can have all the attributes that can reduce memory overhead | Property class object will be to limit the dead, as the case use, even __dict__ will be dispensed with |
__slots__ = ['name'] # 加了以后再添加属性就不行了,限制属性 |
类.mro() |
Find mro order to obtain a list of classes, namely class | In this method have python3 | |
类.__name__ |
Returns the class name of the class | class_name = stu.__class__ |
Built-related object-oriented methods (Python interpreter built-in method)
function | effect | important point | Case |
---|---|---|---|
hasattr() |
Determine whether there is a property of an object | The return value is True / False | hasattr(p, 'age') |
getattr() |
Takes a value from the subject | The third position of the default values, the default value for the absence of | getattr(p, 'age', None) |
setattr() |
To add an object, modify the properties | setattr(p, 'name', 'jackson') |
|
delattr() |
Delete object properties | delattr(p, 'school') |
|
locals() |
Get all the names in the current namespace | 直接修改无效 | 类的初始化函数初始化属性(__init__ ) |
isinstance() |
判断一个对象是不是某个类/某几个类的实例 | 第二参数可以传元组,返回值为 True/ False | isinstance(obj1, (int, Student)) obj1是int 类型或者 Student 类型? |
issubclass() |
判断一个类是不是另一个类的子类 | issubclass(type(obj), Animal) 对象obj 的类 是不是Animal的子类 |
越来越感觉时间不够用了,要加油呀!
一寸光阴一寸金,寸金难买寸光阴。