刚开始玩 BBC micro:bit。其中一个例子有这行代码
flash = [Image().invert()*(i/9) for i in range(9, -1, -1)]
它生成一组图像。为了弄清楚发生了什么,我写了这段代码
class Image:
def __init__(self,*args):
print ("init")
for a in args:
print (a)
def invert(self, *args):
print ("invert")
for a in args:
print (a)
flash = [Image().invert()*(i/9) for i in range(9, -1, -1)]
print ( flash )
产生
python3 test.py
init
invert
Traceback (most recent call last):
File "test.py", line 14, in <module>
flash = [Image().invert()*(i/9) for i in range(9, -1, -1)]
File "test.py", line 14, in <listcomp>
flash = [Image().invert()*(i/9) for i in range(9, -1, -1)]
TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
谢谢