小tip:循环中尽量不要使用多的break和continue,会使分支变多,程序变得复杂,更容易出错
疑问
1.代码中传入错误的参数,和python解释器中传入错误的参数,提示语不一致
def my_abs(x): if not isinstance(x, (int, float)): raise TypeError('bad operand type') if x >= 0: return x else: return -x my_abs('A')
运行结果是;
C:UsersuserDesktop>python C:UsersuserDesktophello.py Traceback (most recent call last): File "C:UsersuserDesktophello.py", line 10, in <module> my_abs('A') File "C:UsersuserDesktophello.py", line 5, in my_abs raise TypeError('bad operand type') TypeError: bad operand type
代码是
def my_abs(x): if not isinstance(x, (int, float)): raise TypeError('bad operand type') if x >= 0: return x else: return -x
解释器中运行,提示语不同
>>> from hello import my_abs >>> my_abs('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:UsersuserDesktophello.py", line 4, in my_abs if not isinstance(x,(int,float)): TypeError: '>' not supported between instances of 'str' and 'int'
参数笔记
参数定义的顺序必须是:必选参数、默认参数、可变参数、命名关键字参数和关键字参数。
https://ai.google/research/pubs/pub62
mapreduce论文