Attributes class attribute 1 2 3 4 5 6 class MyClass: x = 10 def __init__(self): self.y = 20 print(MyClass.x) # 10 like static member variables
getattr and hasattr 1 2 3 4 5 6 7 class MyClass: x = 10 def __init__(self): self.y = 20 print(getattr(MyClass, 'x')) # 10 print(hasattr(MyClass, 'z')) # False assignment to attributes 如果重名,先查看实例的属性
Function calls in class bound method
1 2 3 4 5 6 7 8 class MyClass: def __init__(self, x): self.
Inheritance Object-Oriented Design 代码复现!
注意instance属性可以随时更改,look at the instance, then the subclass before looking at the superclass
Multiple Inheritance 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class A: def __init__(self, x): self.x = x class B: def __init__(self, y): self.y = y def __str__(self): return f"B(y={self.y})" class C(A, B): def __init__(self, x, y): super().
Iterator 1 2 iter(iterable) next(iterator) List list(iterator) 创建一个新的列表,包含迭代器中的所有元素
Dictionary values, keys, items can be iterated using iter() and next() functions
迭代的时候不要改变字典的结构(长度),否则会导致迭代出错
for r in range… if use iterator in for statement, it will not be able to use again, because it will be exhausted after first iteration
built-in functions in Python LAZY MODE:
map / filter / zip / reversed
map / filter filter see data100 😋
zip unpack the zip object into multiple variables(can be useful!