[Tutor] object does not support item assignment

dn PyTutor at DancesWithMice.info
Sun Jul 18 23:24:12 EDT 2021


On 19/07/2021 14.54, Phil wrote:
> I'm in the process of converting a C++ programme that I wrote some time
> ago to Python but I've run into a problem at an early stage.
> 
> The following is a snippet of the code.
> 
> One Led works but how do I work with 8 Leds? led1 =, led2 =, etc and
> then add them to a list? That might be OK for 8 Leds but I think there
> must be a better way.
> 
> TypeError: 'Led' object does not support item assignment
> 
> class Led:
>     def __init__(self, pos):
>         self.pos = pos
> 
>         self.led = Led((50, 50))
> 
>         for i in range(8):
>             self.led[i] = Led((50, 50))


There seems to be two entities here:
- Led: a single light-emitting diode
- Festoon: a series (collection) of Led objects
(which likely are not in an electrical "series"!)

It is worth having an Led class, if you are going to be doing things
with each LED, eg switching it on, switching it off, recording its
state, etc.

It appears as if you would also like to do something with all the
LEDs/Led objects at once, eg turn them all on.

Depending upon this latter point, you may want a separate Festoon class,
or it may be easier to load (however many) individual Led objects into a
list or dict.


class Led:
    def __init__(self, pos):
        self.pos = pos

- and -

my_led = Led((50, 50))

- or -

leds = list()
for i in range(8):
    leds.append( Led((50, 50)) )


Enough to take the next step?
-- 
Regards =dn

-- 
Regards,
=dn


More information about the Tutor mailing list