[Python-Dev] can't assign to function call (original) (raw)

Hrvoje Niksic hrvoje.niksic at avl.com
Mon Mar 18 16:01:12 CET 2013


On 03/18/2013 03:23 PM, Chris Angelico wrote:

The languages that permit you to assign to a function call all have some notion of a reference type.

Assigning to function calls is orthogonal to reference types. For example, Python manages assignment to subscripts without having references just fine:

val = obj[index] # val = obj.getitem(index) obj[index] = val # obj.setitem(index, val)

In analogy with that, Python could implement what looks like assignment to function call like this:

val = f(arg) # val = f.call(arg) f(arg) = val # f.setcall(arg, val)

I am not arguing that this should be added, I'm only pointing out that Python's object customization is not fundamentally at odds with assignment to function calls. Having said that, I am in fact arguing that Python doesn't need them. All C++ uses of operator() overloads can be implemented with the subscript operator.

Even if one needs more different assignments than there are operators, Python can provide it as easily as C++. For example, on std::vector::operator[] provides access to the container without error checking, and std::vector::at() checks bounds:

vec[i] = val // no error checking vec.at(i) = val // error checking

This is trivially translated to Python as:

vec[i] = val # primary functionality, use setitem vec.at[i] = val # secondary functionality, setitem on a proxy



More information about the Python-Dev mailing list