newbie apply() question

Fredrik Lundh fredrik at pythonware.com
Tue Jul 3 03:17:53 EDT 2001


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>





More information about the Python-list mailing list