Language Reference
未分类
Data Types
list
datetime
[ ] calendar
heapq
bisect
通过二分法来查找list或者插入数据
bisect.insort(list, item) # 把x插入list并保持顺序
bisect.bisect(list, item) # 找到可以插入item的位置(最右侧)
# 查看是否存在
def index(a, x):
i = bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
raise ValueError
copy
copy.copy(x): return a shallow copy of x
copy.deepcopy(x): return a deepcopy copy.copy只会copy一层, 里面的可变对象不会copy
copy.deepcopy会copy recursively
在shallow copy里, 对于dict, 使用的是 dict.copy(), 对于list使用的是copied_list = original_list[:]
如果要实现自己的copye, 可以重写__copy__()和__deepcopy__()[ ] pprint
enum
6. Expressions
magic method魔法方法
-
class A:
def __getitem__(self, sli): sli.start, sli.stop, sli.step # A()[start:stop:step]
Evaluation order 执行顺序
运算符 |
描述 |
|---|---|
() |
# 括号内 |
** |
# 指数 |
+x, |
-x # 负数 |
|
比较运算符 |
|
等于运算符 |
=, *=, -=, += |
赋值运算符 |
in, |
not in, is, is not, <, <=, >, >=, != # 比较 |
not |
x # |
and |
# |
or |
and 和 or不是同样的哦。 |
:= |
海象运算符最后,所以要if (a:=3) > 3 |
Simple statements 简单语句
Compound statements 复合语句
with语句 the with statement
测试 成功执行时, exit的三个参数都为None, 否则为对应数据
class A():
def __enter__(self):
print('enter')
def __exit__(self, exc_type, exc_value, traceback):
print(f'exc_type: {exc_type}')
print(f'exc_value: {exc_value}')
print(f'traceback: {traceback}')
print('exist')
with A():
print('start')
raise Exception('value')
for 语句
通过内置变量counter来记录执行的位置,所以remove会导致少执行,insert会导致重复执行
for i in a: if i == 3: a.remove(i) # 少执行 if i == 3: a.insert(0, 3) # 多执行
class
属性
__new__: 创建class类的时候调用
示例. 通过__new__的时候`,返回不同的class
```python
class GuessAnimal(object):
def __name__(self, type, *args, **kwargs):
if type == 'dog':
return Dog(*args, **kwargs)
return Cat(*args, **kwargs)
d = Some("dog")
d.say()
c = Some("cat")
```
* `__module__` : class的模块
* `__name__` : class的name