[Python-ideas] New __reference__ hook
Sturla Molden
sturla at molden.no
Wed Dec 5 19:09:47 CET 2012
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__.
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 "=". This is a big thing, cf. the difference between
NumPy and numexpr:
z = numexpr.evaluate("""a*x + b*y + c""")
The reason numerical expressions must be written as strings to be
efficient in Python is because there is no __bind__ function.
Sturla
More information about the Python-ideas
mailing list