tagging dictionary data
I would like to do something equivalent to "tagging"开发者_如何学JAVA a dictionary element.
Here is some pseudo-code to illustrate the idea:
mydict = dict()
mydict['thiskey'] = 'myvalue'
#then i would like to do this:
mydict['thiskey'].tag = 1
is there anything like this? or maybe i could just append to the value somehow? but i would need to know how to still access the original value...
>>> class MyStr(str):pass
...
>>> mydict = dict()
>>> mydict['thiskey'] = MyStr('myvalue')
>>> mydict['thiskey'].tag = 1
Two possibilities I can think of are either creating a simple TaggedData class containing just a tag and value. Or using a dictionary as the value with only the keys 'tag' and 'value'.
mydict = dict()
mydict['thiskey'] = TaggedObject("myvalue")
mytag = Tag("tagName")
mydict['thiskey'].tag = mytag
So you need to create two classes TaggedObject
and Tag
, but the latter is optional: you can simply use a string and write this:
mydict = dict()
mydict['thiskey'] = TaggedObject("myvalue")
mydict['thiskey'].tag = "tagName"
精彩评论