[Tutor] object does not support item assignment
boB Stepp
robertvstepp at gmail.com
Sun Jul 18 23:19:53 EDT 2021
On Sun, Jul 18, 2021 at 9:55 PM Phil <phillor9 at gmail.com> wrote:
> 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))
Are you sure you don't want something more like this:
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Led:
... def __init__(self, pos):
... self.pos = pos
...
>>> leds = {}
>>> for i in range(8):
... leds[i] = Led((50, 50))
...
>>> leds
{0: <__main__.Led object at 0x0000024347DCCE80>, 1: <__main__.Led
object at 0x0000024347D794C0>, 2: <__main__.Led object at
0x000002434826F6A0>, 3: <__main__.Led object at 0x000002434826F730>,
4: <__main__.Led object at 0x000002434826F400>, 5: <__main__.Led
object at 0x000002434826F490>, 6: <__main__.Led object at
0x000002434826F4F0>, 7: <__main__.Led object at 0x000002434826F550>}
This gives you 8 Led objects to work with that you could address like:
leds[0].turn_on()
leds[1].turn_off()
...
If you had a couple of methods turn_on() and turn_off() in your Led class.
Not really sure what you are aiming for, but this is my best guess.
HTH!
boB Stepp
More information about the Tutor
mailing list