[Python-ideas] New __reference__ hook

Masklinn masklinn at masklinn.net
Wed Dec 5 19:51:12 CET 2012


On 2012-12-05, at 19:09 , Sturla Molden wrote:

> On 05.12.2012 18:05, Jasper St. Pierre wrote:
>> And? What's wrong with an __iadd__ that's exactly the same as Mike's
>> __add__?
> 
> I think it was a Java-confusion. He thought numbers were copied on assignment. But there is no difference between value types and object types in Python. Ints and floats are immutable, but they are not value types as in Java.
> 
> But apart from that, I think allowing overloading of the binding operator "=" might be a good idea. A special method __bind__ could return the object to be bound:
> 
>   a = b
> 
> should then bind the name "a" to the return value of
> 
>   b.__bind__()
> 
> if b implements __bind__.

Sounds odd and full of strange edge-cases. Would bind also get called
when providing parameters to a function call? When putting an object in
a literal of some sort? When returning an object from a function/method?
If not, why not?

> Sure, it could be used to implement copy on assignment. But it would also do other things like allowing lazy evaluation of an expression.
> 
> NumPy code like
> 
>   z = a*x + b*y + c
> 
> could avoid creating three temporary arrays if there was a __bind__ function called on "=".

Why? z could just be a "lazy value" at this point, basically a manual
building of thunks, only reifying them when necessary (whenever that
is). It's not like numpy *has* to create three temporary arrays, just
that it *does*.




More information about the Python-ideas mailing list