Overloadable Assignment PEP

Richie Hindle richie at entrian.com
Thu Apr 3 14:42:28 CEST 2003

> I submitted a pre-PEP on overloadable assignment. 

> Take a step back and tell us what problem you are trying to solve.
> Whatever it is, I'm sure we can think of a better solution than having
> assignment depend on whatever object, if any, happened to be
> previously bound to the same name.

I have a use case for this.  My PyMeld library lets you manipulate HTML as
though it were a collection of Python objects, like this:

>>> from PyMeld import Meld
>>> page = Meld('''<html><body>
... <textarea id="message">Type a message.</textarea>
... </body></html>
... ''')
>>> page.message = "New content."     # Change the content of an element.
>>> print page
<textarea id="message">New content.</textarea>

So the line `page.message = "New message"` changes the content of the tag
with id 'message'.  In this context, it makes sense that assigning to an
object *changes* that object, rather than *replacing* it with another.

What you can't do with PyMeld is this:

>>> message = page.message
>>> message = "Another new message"
>>> print page
<textarea id="message">Another new message.</textarea>

There are workarounds, but over loadable assignment would make this much

Disclaimer: I don't think this is sufficient reason for adding over
loadable assignment to Python (especially if it would slow down every
assignment of anything to anything, which seems likely), but you asked for
a use case and I believe this is one.  8-)

Richie Hindle
richie at entrian.com

More information about the Python-list mailing list