Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden Dictionary Key

I have a dictionary that is passed to me from a function that I do not have access to the code. In this dictionary there is a key called 'time'. I can print d['time'] and it prints the value I expect. However, when I iterate through the dictionary, this key is skipped. Also d.keys() does not include it. If it matters, the other keys are numerical.

How would I recreate this? How do you see hidden keys without knowing the name? Can this be undone?

print type(d) returns <type 'dict'>

like image 264
blindguy Avatar asked Sep 27 '22 17:09

blindguy


1 Answers

Simple reproduction:

class fake_dict(dict):
    def __getitem__(self, item):
        if item == 'time':
            return 'x' 


d = fake_dict({'a': 'b', 'c': 'd'})
assert isinstance(d, dict)
assert 'time' not in d.keys()
print d  # {'a': 'b', 'c': 'd'}
assert d['time'] == 'x'
like image 111
Łukasz Rogalski Avatar answered Nov 04 '22 22:11

Łukasz Rogalski