Return value of an assignment statement?

Jeff Schwab jeff at schwabcenter.com
Thu Feb 21 18:56:55 EST 2008


bruno.desthuilliers at gmail.com wrote:
> On 21 fév, 23:19, John Henry <john106he... at hotmail.com> wrote:
>> On Feb 21, 2:06 pm, Jeff Schwab <j... at schwabcenter.com> wrote:
>>
>>
>>
>>> John Henry wrote:
>>>> On Feb 21, 1:48 pm, John Henry <john106he... at hotmail.com> wrote:
>>>>> On Feb 21, 1:43 pm, mrstephengross <mrstevegr... at gmail.com> wrote:
>>>>>> Hi all. In C, an assignment statement returns the value assigned. For
>>>>>> instance:
>>>>>>   int x
>>>>>>   int y = (x = 3)
>>>>>> In the above example, (x=3) returns 3, which is assigned to y.
>>>>>> In python, as far as I can tell, assignment statements don't return
>>>>>> anything:
>>>>>>   y = (x = 3)
>>>>>> The above example generates a SyntaxError.
>>>>>> Is this correct? I just want to make sure I've understood the
>>>>>> semantics.
>>>>>> Thanks,
>>>>>> --Steve
>>>>> That's true, and I am happy that they decided to make that a syntax
>>>>> error.
>>>> BTW: The less obvious issues when coming from the C world are Python
>>>> syntax like these:
>>>> y = x = 3
>>>> a = 4
>>>> y = x = a
>>>> print x,y
>>>> a = 5
>>>> print x,y
>>> That's the same behavior I would expect in C, on the grounds that C
>>> assignments do bit-wise copies.  What I found confusing at first was
>>> that the same variable will either directly store or merely refer to an
>>> object, depending on the type of the object:
>>>  >>> a = [ 'hello' ]
>>>  >>> y = x = a
>>>  >>> a += [ 'world' ]
>>>  >>> print x, y
>>> ['hello', 'world'] ['hello', 'world']
>> Yep.  Took me a while to realize there is mutable objects, and non-
>> mutable objects.  To be honest, I am still not too comfortable about
>> it.  For instance, I still get nervous for code like:
>>
>> def invoke_some_fct(parent):
>>    y = parent.x
>>    y += [ 'world' ]
>>    print y, parent.x
>>
>> class abc:
>>    def __init__(self):
>>       self.x=[ 'hello' ]
>>       invoke_some_fct(self)
>>       print self.x
>>
> 
> Explicitely using list.extend would make things clearer:
> 
> def invoke_some_fct(parent):
>       parent.x.extend(['world'])

Whether you use += or extend has nothing to do with it.  You omitted the 
relevant part.  Using extend, it would look like:

	y = parent.x
	y.extend(['world'])

The confusing part is that performing an operation on y may or may not 
alter parent.x, depending on whether the initial type of parent.x is 
immutable.  If parent.x is immutable, y is a copy of the value 
represented by parent.x, and modifying y has not effect on the value of 
parent.x.  If (OTOH) parent.x is mutable, then x and y are really 
references to the same object, and modifications to that object via y 
can be observed via x.  In C, you use pointers to get this effect.


> Now there's no reason to feel nervous about this. All you have to
> remember is that Python never copy anything unless explicitely asked
> for.

It's not that simple.  After a statement like:

	a = b

Whether a and b denote the same object depends on what kind of object b 
represented in the first place.



More information about the Python-list mailing list