473,473 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SyntaxError: can't assign to a function call

What gives ?
a = []
def f(): return a
f() [] a.append(3)
f() [3] a += [3]
a [3, 3] f() [3, 3] f() += [4] SyntaxError: can't assign to function call


Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Feb 26 '06 #1
6 24249
Fuzzyman <fu******@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])

which IS quite legal (or, if you know the RHS list has only one item,
f().append(4) is clearer and more effective).
Alex
Feb 27 '06 #2
"Fuzzyman" <fu******@gmail.com> writes:
f() += [4] SyntaxError: can't assign to function call


It's obvious that that line gives you a syntax error. += is the increment
operator overloaded for strings and lists and so on. It changes the lhs in
place appending the rhs. In this case the rhs is a function call so... how the
compiler knows how to assign to a function call?

Do the things easily:

- x = f()
- x += [4]

:)

--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Feb 27 '06 #3

Alex Martelli wrote:
Fuzzyman <fu******@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.

All the best,

Fuzzyman
which IS quite legal (or, if you know the RHS list has only one item,
f().append(4) is clearer and more effective).
Alex


Feb 27 '06 #4
Fuzzyman <fu******@gmail.com> wrote:
...
Exactly what the error message says: it's syntactically forbidden to
perform any assignment on a function-call.
... 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.


Not sure what you mean by 'everywhere else'; generally and syntactically
speaking, you can use a function-call, more or less, in all the places,
and only the places, in which you could use a constant (literal) list
such as [2] -- you can't assign to it, you can't use it as the x in 'for
x in ...', in a clause "except x, y:" you can (syntactically) use it as
x but not as y, etc. Basically, wherever Python needs a rebindable name
or other rebindable reference, you cannot substitute a function call,
nor a constant (literal) list (nor any of several other possible
literals and other expressionforms).

Wherever Python just needs a value, not a rebindable whatever, then of
course you can supply that value in whatever syntax form suits you best,
including a function-call, a literal, and many other ways besides.
Alex
Feb 27 '06 #5
"Fuzzyman" <fu******@gmail.com> wrote:

Alex Martelli wrote:
Fuzzyman <fu******@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, ti**@probo.com
Providenza & Boekelheide, Inc.
Feb 27 '06 #6
In article <on********************************@4ax.com>,
Tim Roberts <ti**@probo.com> wrote:

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.


Actually, it's not quite the same as the expansion, either:
a=[1]
b=a
a+=[2]
a is b 1 a = a + [3]
a is b 0 a [1, 2, 3] b

[1, 2]
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"19. A language that doesn't affect the way you think about programming,
is not worth knowing." --Alan Perlis
Mar 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
5
by: ram | last post by:
Stupid question #983098403: I can't seem to pass an unpacked sequence and keyword arguments to a function at the same time. What am I doing wrong? def f(*args, **kw): for a in args: print...
7
by: Laurent Pointal | last post by:
on win32] Given the following: 45 .... (<generator object at 0x00A79788>,) .... File "<stdin>", line 1 SyntaxError: invalid syntax
34
by: Srinu | last post by:
Hi all, Can we assign return value of a function to a global variable? As we know, main() will be the first function to be executed. but if the above is true, then we have a function call before...
16
by: maehhheeyy | last post by:
this is stopping my program from running properly. is there something wrong in my code when that happens?
1
by: Emile van Sebille | last post by:
Prasad, Mrunalini wrote: Well, what are you trying to do here? SyntaxError:can't assign to operator Emile
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.