why cannot assign to function call

MRAB google at mrabarnett.plus.com
Fri Jan 2 02:04:55 CET 2009


Fuzzyman wrote:
> On Dec 29 2008, 9:34 am, John Machin <sjmac... at lexicon.net> wrote:
>> On Dec 29, 5:01 pm, scsoce <scs... at gmail.com> wrote:
>>
>>> I have a function return a reference,
>> Stop right there. You don't have (and can't have, in Python) a
>> function which returns a reference that acts like a pointer in C or C+
>> +. Please tell us what manual, tutorial, book, blog or Usenet posting
>> gave you that idea, and we'll get the SWAT team sent out straight
>> away.
>>
>>> and want to assign to the
>>> reference, simply like this:
>>>  >>def f(a)
>>>           return a
>> That's not a very useful function, even after you fix the syntax error
>> in the def statement. Would you care to give us a more realistic
>> example of what you are trying to achieve?
>>
>>>      b = 0
>>>     * f( b ) = 1*
>> Is the * at the start of the line meant to indicate pointer
>> dereferencing like in C? If not, what is it? Why is there a * at the
>> end of the line?
>>
>>> but the last line will be refused as "can't assign to function call".
>>> In my thought , the assignment is very nature,
>> Natural?? Please tell us why you would want to do that instead of:
>>
>>     b = 1
>>
>>>  but  why the interpreter
>>> refused to do that ?
>> Because (the BDFL be praised!) it (was not, is not, will not be) in
>> the language grammar.
> 
> Although not being able to do the following has on occasion annoyed
> me:
> 
> f(x) += 1
> 
> If the object returned by f(x) supports in place operations then it is
> an entirely logical meaning, but not the interpreter can't know ahead
> of time whether that is the case or not.
> 
+= always rebinds, even for in-place operations, so, in a sense, it's 
not surprising!

I suppose it's like forbidding assignment within an expression (such as 
an if- or while-condition): annoying sometimes, but a reasonable 
restriction.



More information about the Python-list mailing list