I have a class Commit
.
class Commit:
def __init__(self, uid, message):
self.uid = uid
self.message = message
def __str__(self):
print(self.__dict__)
return textwrap.dedent('''\
Commit: {uid}
{message}
''').format(self.__dict__)
This seems right to me; both keys exists and are non-None
, as seen from the output of the print
call:
{'message': 'Hello, world!', 'uid': 1}
However, the call to str.format()
on the list line raises a KeyError
.
Traceback (most recent call last): File "../Pynewood/pnw", line 7, in cli(sys.argv) File "/Users/daknok/Desktop/Pynewood/pynewood/cli.py", line 11, in cli print(commit) File "/Users/daknok/Desktop/Pynewood/pynewood/commit.py", line 14, in __str__ ''').format(self.__dict__) KeyError: 'uid'
Why am I getting this error, while the keys clearly exist in the dictionary?
vars(self)
. I think it looks nicer thanself.__dict__
– John La Rooy