Newbie: Object assignment return value

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Fri Feb 15 14:24:16 EST 2002


On 14 Feb 2002 10:51:40 -0800, Dean Goodmanson <ponderor at lycos.com> wrote:
>(I think) I'm trying to understand if object assignment has a return value.
>
>Scenario A:
>>>> str( spam= 5 )
>''
>>>> str((spam=5))
>  File "<stdin>", line 1
>    str((spam=5))
>            ^
>SyntaxError: invalid syntax
>>>> #  continued : Scenario B:
>>>> print spam
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>NameError: name 'spam' is not defined
>
>A: Why does including an extra set of paranthesis throw when the original doesn't?
>
>B: What happend to my spam?
>Espcially when this works:
>>>> str(int())
>'0'
>>>> str((int()))
>'0'
>>>>
>
>Wishin'-I-had-something-practical-for-an-example,
>
>Dean

As others have pointed out, assignments are statements.  Expressions can
appear in statements but not vice versa.  Assignments do return values in
the sense that it can be used in chained assignments, but nothing else.

But you examples look strange anyway.  Did you copy and paste verbosely?
Which version of Python are you using?  I get

>>> str( spam= 5 )
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: this function takes no keyword arguments
>>> str(int())
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: int requires at least 1 argument; 0 given
>>> str((int()))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: int requires at least 1 argument; 0 given

Huaiyu



More information about the Python-list mailing list