Overloadable Assignment PEP

Jack Diederich jack at performancedrivers.com
Thu Apr 3 08:20:06 EST 2003


On Thu, Apr 03, 2003 at 01:42:28PM +0100, Richie Hindle wrote:
> 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>
> 

As others have pointed out, this isn't pythonic, but you could make a string 
wrapper class so you could do:
  messgae = page.message
  message.txt = "Another New Message"

you still couldn't do
  foo = message.txt
  foo = "another new message"

You might want to rethink using the magic of page.message finding the element
in page with the 'id' attribute equal to 'message'.  People will undoubtably
start naming their element 'id' attributes to '__init__' or something else
to make your life difficult.

(psuedo python debugging below)

>>> msg = page.byId('message')
>>> print msg['id']
message
>>> print msg.txt
another new message
>>> print page.elements
[<class textarea>,]

I'm not sure what happens when two elements at the same level have the same
'id' attribute (that applies to any way you do it).

-jackdied





More information about the Python-list mailing list