This might be an extremely noob python question however I'm curious.

Is it possible to make right-hand value binding (not sure about terminology) in python?

Here's some code:

>>> d = {'a':1 , 'b': 2 }
>>> d['a']
1
>>> temp = d['a']
>>> temp
1
>>> temp = 4
>>> temp
4
>>> d
{'a': 1, 'b': 2}

Can we use temp as a shortcut of d['a']? So when we do temp = 4, the last line to print {'a': 4, 'b': 2}?

Does any syntax to do it exist or the only way to update the d['a'] value is by d['a'] = 4?

有帮助吗?

解决方案

There is no such feature in Python. You could create an object that represents a reference to the according map entry and that has methods to set the the map's value, but that is not specifically supported by Python syntax.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top