suppose i have object has key ‘dlist0’ with attribute ‘row_id’ the i can access as
getattr(dlist0,'row_id')
then it return value
but if i have a dictionary
ddict0 = {'row_id':4, 'name':'account_balance'}
getattr(ddict0,'row_id')
it is not work
my question is how can i access ddict0 and dlist0 same way
any one can help me?
Dictionaries have items, and thus use whatever is defined as
__getitem__()to retrieve the value of a key.Objects have attributes, and thus use
__getattr__()to retrieve the value of an attribute.You can theoretically override one to point at the other, if you need to – but why do you need to? Why not just write a helper function instead:
Python 2.x:
Python 3.x: