[PEP 203] Augmented Assignment

Frank Niessink frankn=nuws at cs.vu.nl
Mon Aug 14 15:19:18 EDT 2000


Thomas Wouters <thomas at xs4all.net> wrote:

> --7JfCtLOvnd9MIVvH
> Content-Type: text/plain; charset=us-ascii
> Content-Disposition: inline


> I've attached the current draft for the Augmented Assignment proposal. Due
> to popular demand <wink> I've added a Rationale section that tries to
> (*briefly*) explain why augmented assignment is a Good Thing. If you prefer
> HTML over text, you can find the HTML version on the python PEP page:

Maybe a stupid question, but why is there no hook for the regular
assignment in this proposal? The proposal talks about 12 assignment 
operators, of which 11 are 'inplace' and 1 regular. But there is
no hook for the regular assignment. If a hook would be created for 
regular assignment the object can decide what happens if something is
assigned to it.

Silly example: 

class Undoable:
	def __init__(self, value=None):
		self.data = value
		self.history = []

	def __repr__(self):
		return repr(self.data)

	def __assign_ab__(self, value):
		self.history.append(self.data)
		self.data = value

	def undo(self):
		self.data = self.history[-1]
		del self.history[-1]

>>> x = Undoable(1)
>>> print x
1
>>> x = 2
>>> print x
2
>>> x.undo(); print x
1


Cheers, Frank
-- 
He lay, panting heavily in the wet air, and tried feeling bits of himself to
see where he might be hurt. Wherever he touched himself, he encountered a
pain. After a short while he worked out that this was because it was his hand
that was hurting.
	-- Douglas Adams, 'Life, the Universe, and Everything'



More information about the Python-list mailing list