python3注意点
dict set list tuple
tuple是不可变对象。
dict的key值最好使用不可变对象,例如字符串。最好不要用tuple。如果tuple中含有可变对象的话,还是会有错误:TypeError: unhashable type: 'list'。
函数
函数的默认参数最好是不可变对象。
如下:
1 | def add_some(L=[]): |
1 | 调用 |
可变参数
在参数前加*即可使用可变参数,如下。
1 | def calc(*numbers): |
1 | >>>calc(1,2,3,4) |
在list,tuple前加*可以使之变为可变参数传入函数之中。
1 | >>>list1=[1,2,3,4] |
