Cannot update dictionary

I have a dict_ class that attempts to copy the built-in dict class.

Here is that class (the new function returns the original object):

class dict_:
    def __init__(self, *args, **kwargs):
        self.kv = kwargs

        if not self.kv:
            for kv in args:
                for k, v in kv:
                    self.kv.update({k: v})

    def __str__(self):
        return "%s" % self.kv

    def __getitem__(self, item):
        return self.kv[item]

    def update(self, *args):
        self.kv.update(args)

I've called it like this:

from Dodger.dodger import *

term = new(System())
a = new(dict_(a=1, b=2))
a.update(new(dict_(c=3)))
term.println(a)

This is supposed to modify a to {"a": 1, "b": 2, "c": 3} but instead it gives me this error:

Traceback (most recent call last):
  File "C:/free_time/Dodger/dodger_test.py", line 5, in <module>
  File "C:\free_time\Dodger\dodger.py", line 176, in update
  File "C:\free_time\Dodger\dodger.py", line 173, in __getitem__
KeyError: 0

Why is it giving a KeyError? What does the 0 mean? (I am using python 3.8.2)



Read more here: https://stackoverflow.com/questions/67461449/cannot-update-dictionary

Content Attribution

This content was originally published by Alan Bagel at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: