Lazy evaluation: overloading the assignment operator?

Terry Reedy tjreedy at udel.edu
Wed May 2 21:27:49 EDT 2007


"sturlamolden" <sturlamolden at yahoo.no> wrote in message 
news:1178133344.415521.118710 at n76g2000hsh.googlegroups.com...
|
| Python allows the binding behaviour to be defined for descriptors,
| using the __set__ and __get__ methods. I think it would be a major
| advantage if this could be generalized to any object, by allowing the
| assignment operator (=) to be overloaded.

Conceptually, assignment is *not* an operator.  Binary operators take two 
values (of the same type) and produce a third (usually of either the input 
type or a boolean) that usually depends on both inputs.  Assignment does 
nothing of the sort.

In Python, the equal sign is *not* an operator: it is a grammatical symbol. 
One use of the operator fiction in C is to enable simple chained 
assignment: a=b=2.  Python does this directly without the fiction.  C's 
store-and-test usage can be implemented in Python with a 'purse' class.

| One particular use for this would be to implement "lazy evaluation".

Since (in Python, at least) operands are evaluated *before* the 
operator/function is called, I do not see how.

| Should there be a PEP to overload the assignment operator?

You mean a PEP to make assignment an (pseudo)operation and hence 
overloadable (as all operators are).  That has been proposed and rejected 
before, more than once, but I don't have a reference handy.

Terry Jan Reedy






More information about the Python-list mailing list