[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Return value of an assignment statement?

mrstephengross

2/21/2008 9:43:00 PM

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
68 Answers

John Henry

2/21/2008 9:48:00 PM

0

On Feb 21, 1:43 pm, mrstephengross <mrstevegr...@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.

Jeff Schwab

2/21/2008 9:51:00 PM

0

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()

7stud --

2/21/2008 9:52:00 PM

0

On Feb 21, 2:43 pm, mrstephengross <mrstevegr...@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

x = y = 1
print x, y

--output:--
1 1

With parentheses, it looks like python thinks you are trying to do a
boolean == inside the parentheses. It's the same error you get if you
write:

if x = y:
print 'yes'

John Henry

2/21/2008 9:54:00 PM

0

On Feb 21, 1:48 pm, John Henry <john106he...@hotmail.com> wrote:
> On Feb 21, 1:43 pm, mrstephengross <mrstevegr...@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

mrstephengross

2/21/2008 9:57:00 PM

0

> 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)

Yep, this is exactly what I am (was) trying to do. Oh well.... Any
clever ideas on this front?

--Steve

Jeff Schwab

2/21/2008 10:06:00 PM

0

John Henry wrote:
> On Feb 21, 1:48 pm, John Henry <john106he...@hotmail.com> wrote:
>> On Feb 21, 1:43 pm, mrstephengross <mrstevegr...@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']

Jeff Schwab

2/21/2008 10:18:00 PM

0

mrstephengross wrote:
>> 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)
>
> Yep, this is exactly what I am (was) trying to do. Oh well.... Any
> clever ideas on this front?

I worked around it by defining a separate thigamabob with a "result"
property. A method of the thigamabob internally performs the
assignment, and returns a boolean result. The body of each branch in
the tree can then retrieve the result object from the thigamabob.
Here's an example hard-coded to match strings against patterns, but I
think the idea should be extensible to other kinds of assign-and-test
situations.

# Just for the sake of this post.
def do_something(m): pass
def do_other_thing(m): pass
def do_default_thing(): pass

import re

class Matcher(object):
def __call__(self, pattern, string):
self.result = pattern.match(string)

if __name__ == '__main__':

pat = re.compile('some pattern')
match = Matcher()

if match(pat, 'a'):
do_something(match.result)
elif match(pat, 'b'):
do_other_thing(match.result)
else:
do_default_thing()

John Henry

2/21/2008 10:19:00 PM

0

On Feb 21, 2:06 pm, Jeff Schwab <j...@schwabcenter.com> wrote:
> John Henry wrote:
> > On Feb 21, 1:48 pm, John Henry <john106he...@hotmail.com> wrote:
> >> On Feb 21, 1:43 pm, mrstephengross <mrstevegr...@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

hw = abc()

Martin v. Loewis

2/21/2008 10:35:00 PM

0

> Hi all. In C, an assignment statement returns the value assigned.

No. C doesn't have an assignment statement. Instead, in C, assignment
is an expression (just like a binary operation or a function call);
that expression evaluates to the value assigned (i.e. the result is
the value, the assignment is just a side-effect).

What you consider the assignment statement is actually an expression
statement, of the syntax

<expression> <semicolon>

So

x = y;
f();
3+4;

are all the same kind of statement.

> In python, as far as I can tell, assignment statements don't return
> anything:

Right - that's because they are statements. No statement "returns"
a value - except for the return statement, of course, but it doesn't
return it in the sense that you could write

foo = return 44

Because of the confusing meaning of "return", I find it better to
say that expressions evaluate to a value, not that they return
a value.

> The above example generates a SyntaxError.
>
> Is this correct? I just want to make sure I've understood the
> semantics.

Please try to study more on the difference between expressions
and statements.

Regards,
Martin

P.S. Just to confuse matters: GNU C also has statement expressions,
of the form

({ int y = foo (); int z;
if (y > 0) z = y;
else z = - y;
z; })

These are expressions, but allow the expressiveness of statements
(including variable declarations)

Terry Reedy

2/21/2008 11:13:00 PM

0


"Jeff Schwab" <jeff@schwabcenter.com> wrote in message
news:5sudnTSXrZY0aCDanZ2dnUVZ_rKtnZ2d@comcast.com...

| That's the same behavior I would expect in C, on the grounds that C
| 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:

Since names and collection slots always refer to objects, I find the above
confusing. Can you clarify what difference you percieve?

tjr