objdict#
- class objdict(*args, **kwargs)[source]#
Bases:
odict
An
odict
that acts like an object – allow keys to be set/retrieved by object notation.In general, operations that would normally act on attributes (e.g.
obj.x = 3
) instead act on dict keys (e.g.obj['x'] = 3
). If you want to actually get/set an attribute, useobj.getattribute()
/obj.setattribute()
.For a lighter-weight example (an object that acts like a dict), see
sc.dictobj()
.Examples:
import sciris as sc obj = sc.objdict(foo=3, bar=2) obj.foo + obj.bar # Gives 5 for key in obj.keys(): # It's still a dict obj[key] = 10 od = sc.objdict({'height':1.65, 'mass':59}) od.bmi = od.mass/od.height**2 od['bmi'] = od['mass']/od['height']**2 # Vanilla syntax still works od.keys = 3 # This raises an exception (you can't overwrite the keys() method)
Nested logic based in part on addict: mewwts/addict
For a lighter-weight equivalent (based on
dict
instead ofodict
), seesc.dictobj()
.Methods