Overloadable Assignment PEP

Richie Hindle richie at entrian.com
Thu Apr 3 07:42:28 EST 2003


[Drew]
> I submitted a pre-PEP on overloadable assignment. 

[Anders]
> 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
<html><body>
<textarea id="message">New content.</textarea>
</body></html>

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
<html><body>
<textarea id="message">Another new message.</textarea>
</body></html>

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

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