why cannot assign to function call

Discussion in 'Python' started by scsoce, Dec 29, 2008.

  1. scsoce

    scsoce Guest

    I have a function return a reference, and want to assign to the
    reference, simply like this: return a
    b = 0
    * f( b ) = 1*
    but the last line will be refused as "can't assign to function call".
    In my thought , the assignment is very nature, but why the interpreter
    refused to do that ?

    scsoce, Dec 29, 2008
    1. Advertisements

  2. scsoce

    r Guest

    because all you need to do is
    trying to change the return value of a function before you even "know
    what it is" so to speak, defeats the whole purpose of sending it there
    in the first place. just assign the variable to a new value. Thats my
    common sense answer, maybe somebody can give a technical one :)
    r, Dec 29, 2008
    1. Advertisements

  3. scsoce a écrit :
    You have a function that returns an object. You can't "assign" to an
    object - this makes no sense.

    I'm afraid you are confusing Python's name/object bindings with C
    pointers or C++ references.
    Bruno Desthuilliers, Dec 29, 2008
  4. scsoce

    John Machin Guest

    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
    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?
    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?
    Natural?? Please tell us why you would want to do that instead of:

    b = 1
    Because (the BDFL be praised!) it (was not, is not, will not be) in
    the language grammar.
    John Machin, Dec 29, 2008
  5. Probably the closest thing you are going to get in Python would be the
    .... pass
    ........ return a

    But as others have pointed out, Python is not C/C++, and shouldn't be
    treated as such.
    anthony.tolle, Dec 29, 2008
  6. scsoce

    Aaron Brady Guest

    'Why' is a long question. The syntax has advantages and disadvantages
    (pros and cons), which weigh different amounts in different
    languages. In Python, the cons weigh more. In C, the pros weigh
    more. The short answer is, there is no such thing as assigning to
    objects, only to variables.

    You are talking like it could save you ten lines of code or something.
    Aaron Brady, Dec 29, 2008
  7. scsoce

    Aaron Brady Guest

    --Why would he take the time to carve "ARGHHH!"?
    --Maybe he was dictating.
    Aaron Brady, Dec 29, 2008
  8. scsoce

    Terry Reedy Guest

    Perhaps the ones claiming that Python is 'call by reference' and hence,
    by implication, at least, 'return by reference'.
    I and others have posted many times that such a viewpoints leads to
    confusion, such as in this post.

    Terry Reedy, Dec 29, 2008
  9. scsoce

    Fuzzyman Guest

    Although not being able to do the following has on occasion annoyed

    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.

    Michael Foord
    Fuzzyman, Jan 2, 2009
  10. scsoce

    MRAB Guest

    += 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
    MRAB, Jan 2, 2009
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.