newbie apply() question

xtian at hyperactive.co.nz xtian at hyperactive.co.nz
Tue Jul 3 18:30:04 EDT 2001


On Tue, 03 Jul 2001 07:17:53 GMT, "Fredrik Lundh"
<fredrik at pythonware.com> wrote:

>Aaron Edsinger wrote:
>> hi.  i'm stumped on something that seems easy.  why doesn't this work:
>>
>> x=4
>> apply(sys.stdout.write,x) #fails
>
>TypeError: apply() 2nd argument must be a sequence
>
>as the exception implies, the second argument to apply
>must be a sequence.  an integer is not a sequence.
>
>> apply(sys.stdout.write,(x,)) #fails
>
>TypeError: argument must be string or read-only character buffer, not int
>
>as the exception implies, the argument (to sys.stdout.write)
>must be a string, not an int(eger).
>
>> is there a way to make this work?
>
>if you really need to use apply, you can do something like:
>
>    apply(sys.stdout.write,(str(x),))
>
>or even:
>
>    sys.stdout.write(*(str(x),))
>
>but since write doesn't take a variable number of arguments,
>I see no reason why you cannot simply write:
>
>    sys.stdout.write(str(x))
>
>hope this helps!
>
></F>

Okay, that's weird. I feel silly - this is almost bound to be the real
problem the poster was having (at least the advice to post tracebacks
is still good!). 
But when I do this in IDLE, I don't get an error - is the IDLE stdout
object automatically calling str() on anything it's passed, while the
raw interpreter doesn't?
Yup - in OutputWindow.OutputWindow.write(). <sigh>
(If only I launched the real interpreter instead of IDLE by default!)

xtian



More information about the Python-list mailing list