let's say I have a class that behaves as an int to the outside world, but in fact is a mutable object that has a fixed place within several structures. if n is my object, I can update it now by writing n.update(34)
, but I find I'm always wanting to write n = 34
instead. I've glanced over http://docs.python.org/reference/datamodel.html, and don't see anything like an __assign__ or __value__ attribute that would let me trap the =
operator and allow me to update my object rather than assign an int to the variable n, throwing my object away. better still, when a mutable is inside a tuple t = (m, n)
, I would like to update my mutable with t[1] = 34
, but Python sees that as attempting to modify an immutable.
so I guess the answer would be, based upon what little of that document I was able to grok, that what I'm attempting is not Pythonic. so if I can't do it the way I want, what would be the next best thing? such that if someone has to maintain my code someday, it won't be overly offensive.