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

"Martin v. Löwis" martin at v.loewis.de
Fri Aug 11 21:48:43 CEST 2006


Neal Becker schrieb:
>> No. Array references (x[i]) and attribute references (x.a) represent
>> "locations". Function calls represent values. This is no different
>> than the distinction between lvalues and rvalues in C.
>>
> 
> Except this syntax is valid in c++ where X() is a constructor call:
> 
> X(whatever) += 2; is (or can be) valid c++

That's actually the less-interesting case. You would have to overload
+= to make it work, right?

The more interesting case is when X is a function that returns a
reference:

int& X(int);

void foo(){
  X(1) += 2;
}

int bar, foobar;
int& X(int t){
  if(t)  return bar;
  return foobar;
}

Here, which variable gets incremented depends on whether the t
argument is true; no overloading of assignment comes into play.

The trick is that C++ has functions that *return* lvalues;
neither C nor Python has such things.

Regards,
Martin


More information about the Python-Dev mailing list