pstar
.defaultpdict
.__getitem__(self, key)
Subscript operation. Keys can be any normal dict
keys or list
s of such keys.
Examples:
pd = defaultpdict(int).update(foo=1, bar=2.0, baz='three')
assert (pd['foo'] == pd.foo == 1)
assert (pd[['foo', 'bar', 'baz']].aslist() == [1, 2.0, 'three'])
When indexing with a list
, the returned plist
is rooted at a plist
of
KeyValue
namedtuple
s, making it easy to recover the keys that gave the values, and
allows the plist
to be turned back into a corresponding pdict
:
assert (pd[['foo', 'baz']].root().aslist() ==
[('foo', 1), ('baz', 'three')])
assert (pd[['foo', 'baz']].pdict() ==
dict(foo=1, baz='three'))
Args:
key
: Anyhash
able object, or alist
ofhash
able objects.
Returns:
Either the value held at
key
, or aplist
of values held at each key in thelist
of keys, when called with alist
of keys.