python学习笔记
小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:\Users\user\Desktop>python C:\Users\user\Desktop\hello.py
Traceback (most recent call last):
File "C:\Users\user\Desktop\hello.py", line 10, in <module>
my_abs('A')
File "C:\Users\user\Desktop\hello.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:\Users\user\Desktop\hello.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论文

浙公网安备 33010602011771号