Return value of an assignment statement?

Jeff Schwab jeff at schwabcenter.com
Thu Feb 21 16:51:03 EST 2008


mrstephengross 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.

Yes, but there is valid syntax for the common case you mentioned:

     y = x = 3

What you can't do (that I really miss) is have a tree of assign-and-test 
expressions:

	import re
	pat = re.compile('some pattern')

	if m = pat.match(some_string):
	    do_something(m)
	else if m = pat.match(other_string):
	    do_other_thing(m)
	else:
	    do_default_thing()



More information about the Python-list mailing list