SyntaxError: can't assign to a function call

Discussion in 'Python' started by Fuzzyman, Feb 26, 2006.

  1. Fuzzyman

    Fuzzyman Guest

    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
     
    Fuzzyman, Feb 26, 2006
    #1
    1. Advertising

  2. Fuzzyman <> 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
     
    Alex Martelli, Feb 26, 2006
    #2
    1. Advertising

  3. "Fuzzyman" <> 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"
     
    Lawrence Oluyede, Feb 26, 2006
    #3
  4. Fuzzyman

    Fuzzyman Guest

    Alex Martelli wrote:
    > Fuzzyman <> 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
     
    Fuzzyman, Feb 26, 2006
    #4
  5. Fuzzyman <> 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
     
    Alex Martelli, Feb 27, 2006
    #5
  6. Fuzzyman

    Tim Roberts Guest

    "Fuzzyman" <> wrote:

    >
    >Alex Martelli wrote:
    >> Fuzzyman <> 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,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Feb 27, 2006
    #6
  7. Fuzzyman

    Aahz Guest

    In article <>,
    Tim Roberts <> 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 () <*> http://www.pythoncraft.com/

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

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd
    Replies:
    1
    Views:
    323
    Erik Max Francis
    Dec 23, 2005
  2. Melih Onvural

    SyntaxError: 'return' outside function

    Melih Onvural, Jan 31, 2007, in forum: Python
    Replies:
    3
    Views:
    1,700
    Melih Onvural
    Feb 1, 2007
  3. Albert Hopkins
    Replies:
    3
    Views:
    492
    Steven D'Aprano
    Dec 10, 2008
  4. scsoce
    Replies:
    9
    Views:
    3,619
  5. Miles
    Replies:
    138
    Views:
    2,402
    Terry Reedy
    Mar 15, 2009
Loading...

Share This Page