Lazy evaluation: overloading the assignment operator?

Stargaming stargaming at gmail.com
Wed May 2 15:46:59 EDT 2007


sturlamolden wrote:
> Python allows the binding behaviour to be defined for descriptors,
> using the __set__ and __get__ methods. 

AFAIK, __getattribute__ calls them *explicitly*.

> I think it would be a major
> advantage if this could be generalized to any object, by allowing the
> assignment operator (=) to be overloaded.

> 
> One particular use for this would be to implement "lazy evaluation".
> For example it would allow us to get rid of all the temporary arrays
> produced by NumPy.
> 
> For example, consider the expression:
> 
[snip]
> 
>  y = a * b + c * d
> 
> would then result in something like this:
> 
> tmp1 = LazyExpr('__mul__',a,b) # symbolic representation of "a * b"
> tmp2 = LazyExpr('__mul__',c,d) # symbolic representation of "c * d"
> tmp3 = LazyExpr('__add__',tmp1,tmp1) # symbolic "a * b + c * d"
> del tmp1
> del tmp2
> y = tmp3 # tmp3 gets evaluated as assignment is overloaded
> 
To allow lazy evaluation, you need overloading of the assignment 
operator? Where should you overload it? y is less than None when you do 
that assignment. I don't really see the need for overloading here. 
Following the binding rules, __mul__ would (even without any hackery) be 
evaluated before __add__.

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

If -- after this discussion -- community seems to like this feature, you 
could try to come up with some patch and a PEP. But not yet.

> In terms of
> syntax, it would not be any worse than the current descriptor objects
> - but it would make lazy evaluation idioms a lot easier to implement.

-- 
Stargaming



More information about the Python-list mailing list