0

Just started playing with a BBC micro:bit. One of the examples has this line of code

flash = [Image().invert()*(i/9) for i in range(9, -1, -1)]

It generates set of images. In trying to figure out what is going on I wrote this piece of code

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 )

which produces

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'

Thanks

nekomatic
  • 5,988
  • 1
  • 20
  • 27
user667522
  • 29
  • 4

3 Answers3

0

Your function invert isn't returning anything so when you try to multiply it you have None*float resulting in your described answer.

kpie
  • 9,588
  • 5
  • 28
  • 50
0

In invert() You need to pass some int values and return any int value. In your code you are not returning any int or float value in your invert() function. Try this

class Image:
    def __init__(self,*args):
        print ("init")
        for a in args:
            print (a)

    def invert(self, *args):
        print ("invert")
        for a in args:
            return a
flash = [Image().invert(1,)*(i/9) for i in range(9, -1, -1)]
print (flash)

This will work

  • This just returns the first argument. I don't know what `invert` is supposed to do, but this just does `return args[0] if args else None`. – tdelaney Feb 15 '20 at 20:52
0

Just discovered Image has a * operator, ie its not the unpack operator, which was the thing that was confusing me.

Thanks for the responses

user667522
  • 29
  • 4