Need a metavalue singleton kinda thingy as a reserved key...

Noah noah at noah.org
Sun Aug 11 08:31:24 CEST 2002


I have a class where you can add values to a dictionary.
I want to have a special meta key that I can use to reserve a spot
in the dictionary. The problem is that the keys in the dictionary
can be almost anything including None, so I can't use None as my
reserved value. Plus I may want to defined more than one reserved key. 
Is there a smarter way to create meta values to denote reserved keys?

The following seems to work, but it looks too crufty. 
The class interface is clear enough, but if someone looks
at the class code they might wonder what I was thinking.
Perhaps I don't understand what I'm thinking.
Am I thinking about this wrong?

class RESERVED:   # This represents a meta key.
    pass

class foo:
    def __init__  (self):
        self.dic = {}
        self.dic[RESERVED] = 0

f = foo()
f.dic['one'] = 1
f.dic['two'] = 2
f.dic[None] = -1
print f[None], f['one'], f['two'], f[RESERVED]

Yours,
Noah



More information about the Python-list mailing list