why cannot assign to function call

Miles semanticist at gmail.com
Mon Dec 29 19:06:37 EST 2008


On Mon, Dec 29, 2008 at 1:01 AM, scsoce <scsoce at gmail.com> wrote:
> I have a function return a reference, and want to assign to the reference,
> simply like this:
>>>def f(a)
>         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 ?

Here's some links to help you better understand Python objects:

http://effbot.org/zone/python-objects.htm
http://effbot.org/zone/call-by-object.htm

The second one is a bit denser reading, but it's important to learn
that Python's approach to objects and "variables" is fundamentally
different from that of C/C++.  In the example below, there's no way in
the Python language* that bar() can change the value of b, since
strings and numbers are immutable.

def foo():
    b = 0
    bar(b)
    print b # will always be 0

* There are stupid [ctypes/getframe/etc.] tricks, though I think all
are implementation-specific

-Miles



More information about the Python-list mailing list