I'm afraid that "fairly new to Python" is rather an understatement

That script is a weird mix of C and Python.
The biggest error is perhaps your misunderstanding of the
__str__ method which should return a pretty string showing the entire object. But you never create an object instance, so
self (the reference to an instance) doesn't exist. There are numerous other errors so I suggest you follow an online course or buy a book to learn the language. As a C programmer you'll find it easy. The following script does something akin to what you're after:
Code: Select all
class CommandCodes:
Death = const(0x0001)
Life = const(0x0002)
Universe = const(0x0040)
Everything = const(0x0042)
def __init__(self): # Need an instance to create the dictionary - or could use a classmethod
self.d = {self.Death:'Death', self.Life:'Life', self.Universe:'Universe', self.Everything:'Everything'}
def __str__(self):
s = ''
for key in sorted(self.d.keys()):
ss = '{:04x} : {}'.format(key, self.d[key])
''.join((s, ss))
print(s, ss)
return s
def __getitem__(self, key): # Enable dict-like access
return self.d[key]
command_codes_instance = CommandCodes()
print(str(command_codes_instance)) # The entire object
print(command_codes_instance[Everything]) # A single value
print(CommmandCodes.Everything) # Don't need an instance here
One point which may seem trivial but isn't is this. Use spaces rather than tabs. Python is strict about indentation. Using tabs means that one day you'll inadvertently mix spaces and tabs getting code which looks good but won't compile. The
standard is four spaces.