Stupid string formatting question

Delaney, Timothy tdelaney at avaya.com
Thu May 16 21:41:29 EDT 2002


> From: Michael S. Fischer [mailto:michael+usenet at dynamine.net]
> 
> Why can't I do this?
> 
>     def foo(x):
>       # Do some operations on x yielding a and b
>       return a, b
> 
>     print "a = %s, b = %s" % (foo(x))
> 
> It seems counter-intuitive that although foo is defined to 
> return a 2-item
> tuple, the interpreter just doesn't get it.

def foo(x):
    # Do some operations on x yielding a and b
    a = 1
    b = 2
    return a, b

print "a = %s, b = %s" % (foo(x))
                              ^
---------- Run ----------
Traceback (most recent call last):
  File "C:\python\modules\test9.py", line 40, in ?
    print "a = %s, b = %s" % (foo(x))
NameError: name 'x' is not defined
-------------------------

print "a = %s, b = %s" % (foo(1))

---------- Run ----------
a = 1, b = 2
-------------------------

Tim Delaney





More information about the Python-list mailing list