In Lua you can address keys of the dictionary like attributes:
d = {a = 1, b = 1}
d['a'] == d.a -- this returns true
Is there something like that in Python? I've noticed that sklearn bunch objects have similar properties.
In Lua you can address keys of the dictionary like attributes:
d = {a = 1, b = 1}
d['a'] == d.a -- this returns true
Is there something like that in Python? I've noticed that sklearn bunch objects have similar properties.
No, the only valid ways to access a dict
using a key are d['a']
or d.get('a')
The former may raise a KeyError
if that key is not in the dict
, and the latter takes a second optional argument that it will return if the key is not found.
You can make a class and use setattr to access attributes that way, though it seems kinda excessive just for convenience. You could do it with any old object too, but I wouldn't recommend it.