运算符
# 整除17 // 3# 乘方2 ** 7
字符串
r是raw的缩写,字符串不转移。
三引号字符串会包含换行,不想包含在行尾写 \
r'a\a'"""asd \"""s = 'asd'# 重复s * 3s[:2]
切片
+---+---+---+---+---+---+| P | y | t | h | o | n |+---+---+---+---+---+---+0 1 2 3 4 5 6-6 -5 -4 -3 -2 -1
小于-len()和-len()作用一样
s[-7:] == s[-6:]
global关键字
g = 4def foo():global gg = 2foo()print(g)# 输出# 2
表达式
比较运算符
#比较是否是一个对象,也就是id()函数返回是否相等。a = [1, 2]b = aprint(a is b)print(a is not b)TrueFalse
#值相等a = [1 ,2]b = [1, 2]print(a == b)print(a is b)TrueFalse
4 流程控制
4.1 if
# 返回Goodbye"Hello" if 0 else "Goodbye"# 返回Hello"Hello" if 1 else "Goodbye"
4.6 函数
args和*kwargs
def foo(*args, **kwargs):print(args)print(kwargs)foo(1, 2, x=3, y=4)#输出#(1, 2)#{'x': 3, 'y': 4}d = {'x': 3, 'y': 4}foo(**d)
装饰器(decorator)
一阶用法
def log(fn):def wrapper(*args, **kwargs):print('called %s()' % fn.__name__)return fn(*args, **kwargs)return wrapper@logdef say():print('haha')say()#输出#called say()#haha#等价于 say = log(say)
二阶用法
def log(text):def decorator(fn):def wrapper(*args, **kwargs):print('%s %s' % (text, fn.__name__))return fn(*args, **kwargs)return wrapperreturn decorator@log('execute')def say():print('haha')say()#输出#execute say#haha#等价于 say = log('execute')(say)
双下函数
如果没有实现
repr,当我们在控制台里打印一个向量的实例时,得到的字符串
可能会是 。
bool(x) 的背后是调用
x.bool() 的结果;如果不存在 bool 方法,那么 bool(x) 会
尝试调用 x.len()。若返回 0,则 bool 会返回 False;否则返回
True。
seq[start:stop:step] 进行求值的时候,Python 会调用
seq.getitem(slice(start, stop, step))。
双下函数的思想有点类似其他语言里的interface和trait
5 数据结构
5.3 元组
# 单元元素元组tup1 = (1,)tup2 = (3, 4)new_tup = t1 + t2
5.4 集合
5.5 字典
初始化
author = { 'id': 1 }author = dict(id=1)#注意容易出错key = 'id'author = { key : 1} # {'id': 1}author = dict(key = 1) # {'key': 1}
取值
author['name'] #KeyErrorauthor.get('name') #None
遍历
迭代器
for k in d:print(k)for k, v in d.items():print(k, v)
*进阶用法
解构
列表里的元组、字典都可以解构。
d = {'x': 1, 'y': 2, 'z': 3}for k, v in d.items():print(k, v)# 输出# x 1# y 2# z 3for x, y, z in [(1, 2, 3), (4, 5, 6)]:print(x, y, z)# 输出# 1 2 3# 4 5 6def foo(**kwargs):print(kwargs)#形式参数必须和字典的key一一对应def bar(x, y):print(x, y)d = {'x': 1, 'y': 2}foo(**d)bar(**d)#输出#{'x': 1, 'y': 2}#1 2# 字典合并dm = {**d1, **d2}
6 模块
import sound.effects.echosound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
使用from可以不使用包前缀
from sound.effects import echoecho.echofilter(input, output, delay=0.7, atten=4)
import subpackage1.a # 将模块subpackage.a导入全局命名空间,例如访问a中属性时用subpackage1.a.attrfrom subpackage1 import a # 将模块a导入全局命名空间,例如访问a中属性时用a.attr_afrom subpackage.a import attr_a # 将模块a的属性直接导入到命名空间中,例如访问a中属性时直接用attr_a
绝对路径
相对路径
from .foo import bar
.当前模块 ..上层模块 …上上层,以此类推。
报错解决
ValueError: attempted relative import beyond top-level package
6.4 包
init.py
init.py 里面可以写import时执行的代码
all设置后import * 只会导入指定模块
__all__ = ["echo", "surround", "reverse"]from sound.effects import *
8 错误和异常
把try中没必要的代码放到else里
try:raise ValueError('a')except ValueError as e:print(e)except Exception as e:print('e' + e)else:print('haha')
9 类
继承
class Base:passclass Foo(Base):def __init__():super().__init__():#类属性bar = 123# 菱形继承会调用多重,super则不会class Bar(Base):def __init__():Base.__init__(self)
MRO列表
Foo.__mro__
new
9.8 迭代器
在函数参数中复制传值传递,既函数内迭代器状态变化不会影响作用域外。
9.9 生成器
执行生成器函数返回迭代器
def reverse(data):for index in range(len(data)-1, -1, -1):yield data[index]for char in reverse('golf'):print(char)
9.10 生成器表达式
节省内存,不会生成真正的列表。
for tshirt in ('%s %s' % (c, s) for c in colors for s in sizes):print(tshirt)
classmethod和staticmethod
对多态支持不同。
class Foo:@classmethoddef class_bar(cls):pass@staticmethoddef static_bar():pass
https://www.zhihu.com/question/20021164
复合语句
with
class Foo:def __enter__(self):print('into enter')return selfdef __exit__(self, exc_type, exc_val, exc_tb):print(exc_type, exc_val, exc_tb)print('into exit')def run(self):raise Exception('hehe')with Foo() as f:f.run()
as 后面的变量是 enter()的返回值。
