[Tutor] Re: small problem with lists/tuples

Jeff Shannon jeff at ccvcorp.com
Mon Sep 29 16:42:26 EDT 2003


pan at uchicago.edu wrote:


> The following simple trick might work:

>>>>def unpack(target):
>>>
> .. 	tmp = str(target)
> .. 	tmp = tmp.replace('[','').replace(']','')
> .. 	tmp = tmp.replace('(','').replace(')','')
> .. 	return eval(tmp)
> .. 


I strongly dislike the idea of using eval() in this case.  This is, to
my mind, approximately similar to peeling potatoes with a battleaxe.
(Or possibly to killing roaches with mustard gas.)  There are very few
situations where eval() is the best answer to the problem, and many
situations where it will cause a lot of grief.  eval() should almost
always be (at least close to) the last option you look at, not the first.

In addition, this function does not properly respond to the full range
of possible input.  First, a single-element tuple will have not only
parentheses, but also a comma (i.e., '(item,)').  You could add
another replace() to eliminate commas, true...  but what happens if
the inner data item contains commas, or brackets, or parens?  You may
end up mangling the item that you're actually interested in.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list