[Tutor] Alternative File I/O for Tuples (fwd)

Jacob S. keridee at jayco.net
Fri Jul 1 19:09:03 CEST 2005

----- Original Message ----- 
From: "Don Parris" <webdev at matheteuo.org>
To: <tutor at python.org>
Sent: Thursday, June 30, 2005 12:23 AM
Subject: Re: [Tutor] Alternative File I/O for Tuples (fwd)

> On Wed, 29 Jun 2005 14:09:41 -0400
> Kent Johnson <kent37 at tds.net> wrote:
>> Don Parris wrote:
>> > ### playing with wrapfunc (all other args are the same) ###
>> > wrapfunc=lambda x:wrap_onspace(str(rows), x))
>> >
>> > also
>> > wrapfunc=lambda x:str(wrap_onspace(rows, x)))
>> This is way off base. wrap_onspace takes two arguments - the string to
>> wrap, and the width to wrap to. You are passing it two arguments - the
>> tuple of tuples to print, and the string to wrap.
>> >
> Success!!!
>    mbrPhone = open('mbrPhone.txt', 'w')
>    mbrPhone.write(indent(Results, hasHeader=False, separateRows=False,
>    prefix='', postfix='', justify='left', wrapfunc=lambda x:str(x)))
>    mbrPhone.close()

Just a point.

You don't need the extra lambda at wrapfunc.

Writing it as...

mbrPhone.write(indent(Results, hasHeader=False, separateRows=False,
prefix='', postfix='',justify='left',wrapfunc=str))

will work fine.

A lambda definition is simple, and you already grasp most of the concept, 
but the above shows a little needed tweaking.

def a(b,c=d):
    return somefunc(e)

is equivalent to

a = lambda b,c=d: somefunc(e)


def wrapfunc(x):
    return str(x)

can be shortened to

wrapfunc = lambda x:str(x)

which can be shortened even further to

wrapfunc = str

I guess the moral of the story is--why bother making wrapfunc a function 
when you can set it equal to a function -- str --  and it's still the same?

Okay, I'm done.


More information about the Tutor mailing list