python - I want to subclass dict and set default values -
i have need create special subclass of dict. in want set default values set of keys.
i seem failing in finding correct syntax this.
here have been trying:
class newdict(dict): key1 = "stuff" key2 = "other stuff" nolist = [] nada = none i instantiating object this:
prefilleddict = newdict() and trying use in there:
print prefilleddict['key1'] but seems dictionary not dictionary.
what little bit missing?
you can achieve want such:
class newdict(dict): def __init__(self): self['key1'] = 'stuff' ... prefilleddict = newdict() print prefilleddict['key1'] with code, creating attributes of newdict class, not keys in dictionary, meaning access attributes such:
prefilleddict = newdict() print prefilleddict.key1
Comments
Post a Comment