Overloadable Assignment PEP

Erik Max Francis max at alcyone.com
Fri Apr 4 17:36:44 EST 2003


Drew Moore wrote:

> Can we follow up on this a little?
> I'm trying to understand this concern in more detail..
> 
> I'd like this PEP to go one of two ways..
> either implementation,
> or a good solid technical reason why its a bad idea.
> 
> so far, the response seems to be:
> "it is technically possible, but my scripts might
> run slower.. and it just makes me nervous."

Well, your scripts would run slower, but I think the real concern is
about the readability problems this might cause.

> I suspect there is some basis for this nervousness, but
> I still haven't seen an example of how this proposed feature
> would break things.
> 
> since no current code uses __assign__,
> nothing would break immediately.
> 
> however, once it started to be used.. what would go wrong??

It isn't that it wouldn't break preexisting code, it's that after its
introduction, suddenly previously meaningful code can take on entirely
new meanings.  And since Python is dynamic, there's no way to know in
other code how this change might affect it.  Take for instance:

	def f(x=0):
	    if not x:
	        x = 1
	    # ... do something with x

While at first this appears a contrived example, this kind of thing
happens all the time -- say, you're getting some input externally, and
you're clamping it to within bounds, for example.  It is simple and safe
because you can trust in the "x = 1" statement rebinding the _local
name_ x and not having any effect outside the scope of the function call
f.

Now let's say your proposal is passed and implemented.  Now this "x = 1"
statement might _not_ do the rebinding after all.  One would hope that a
well-behaved __assign__ method would make this all okay, but then again
the point of an __assign__ method is that you can make it do whatever
you want.

The down side isn't that this will immediately break huge amounts of
code, it's that suddenly very simple, almost ubiquitous code can
suddenly take on a subtly different meaning, and because of Python's
dynamicism, it's hard to say when the effect will change the meaning.

I suspect this falls into the same class of "Okay this changes the
meaning of everything" like lazy evaluation or macros.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ If the sky should fall, hold up your hands.
\__/ (a Spanish proverb)
    The laws list / http://www.alcyone.com/max/physics/laws/
 Laws, rules, principles, effects, paradoxes, etc. in physics.




More information about the Python-list mailing list