SyntaxError: can't assign to a function call

Tim Roberts timr at probo.com
Mon Feb 27 01:25:28 EST 2006


"Fuzzyman" <fuzzyman at gmail.com> wrote:

>
>Alex Martelli wrote:
>> Fuzzyman <fuzzyman at gmail.com> wrote:
>>
>> > What gives ?
>>    ...
>> > >>> a = []
>> > >>> def f():
>> >       return a
>>    ...
>> > >>> f() += [4]
>> > SyntaxError: can't assign to function call
>>
>> Exactly what the error message says: it's syntactically forbidden to
>> perform any assignment on a function-call.
>>
>> If you're keen on these semantics, use for example
>>
>> f().extend([4])
>>
>
>Cool, thanks. That's what I did, it's just not an error I'd seen
>before. Everywhere else Python evaluates the function call and then
>does it's stuff with the result.

One thing that can be helpful in situations like this is to remember that
+= in Python isn't quite as "special" as it is in C.  So,

  f() += [4]

is the same as

  f() = f() + [4]

and I think you can see why that is a problem.
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list