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'>
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With