[Tutor] Working with files, Truckers log
Alexandre Ratti
alex@gabuzomeu.net
Sat, 06 Apr 2002 19:48:16 +0200
Hello Jeff,
At 12:00 06/04/2002 -0500, you wrote:
>Date: Fri, 05 Apr 2002 13:56:41 -0800
>From: "Jeff Shannon" <jeff@ccvcorp.com>
>Subject: Re: [Tutor] Working with files, Truckers log
> > If you store your values in a text file, for instance one per line, it will
> > be fairly simple to load them back and append them to a list. You can
> > use eval() to transform a string value into a decimal value.
>
>Why on earth should eval() be used for this, when there's perfectly good
>conversion functions??
>
> >>> mylist = ['3.75\n', '8.5\n', '6.0\n']
> >>> # to simulate lines that have been
> >>> # read in from file.readlines()
> >>> [ float(x) for x in mylist ]
>[3.75, 8.5, 6.0]
> >>>
Yes, you are correct. Somehow I missed the obvious solution (oh well).
>There really is almost never a reason to use eval(). Really. :)
How about this kind of use:
def factoryFromName(className, *args):
"className is a string."
return eval(className)(*args)
Admittedly, I can't think up any reason to use this code in a real app
right now :-)
Cheers.
Alexandre