[Python-Dev] can't assign to function call

Chris Angelico rosuav at gmail.com
Mon Mar 18 15:23:07 CET 2013

On Tue, Mar 19, 2013 at 12:50 AM, Neal Becker <ndbecker2 at gmail.com> wrote:
> def F(x):
>     return x
> x = 2
> F(x) = 3
>     F(x) = 3
> SyntaxError: can't assign to function call
> Do we really need this restriction?  There do exist other languages without it.

The languages that permit you to assign to a function call all have
some notion of a reference type. In C++, for instance, you can return
a reference to an object, and assigning to the function call achieves
the same thing as assigning to the referent. C has similar semantics
with pointers; you can dereference a returned pointer:

int somevalue;
int *F() {return &somevalue;}

*F() = 5; /* will assign to somevalue */

With Python, there are no pointers, there are no variables. But you
can do something somewhat similar:

>>> x = [0]
>>> def F():
	return x

>>> F()[0]=3;
>>> x[0]

If you think of x as a pointer to the value x[0], then Python lets you
"dereference" the function return value. But this is fiddling with
terminology; the concept of assigning to a function return value
doesn't really make sense in Python.

Further discussion on exactly _why_ this is the case can be found on
python-list's or python-tutor's archives, such as this excellent post
by Steven D'Aprano:


TLDR: Python != C. :)


More information about the Python-Dev mailing list