Puzzle: anonymous tuple unpacking w/o eval

Edward K. Ream edreamleo at charter.net
Fri May 16 08:29:43 EDT 2003


For a long time I've wondered how to generalize tuple unpacking, such as:

a,b,c = (val1,val2,val3)

so that it isn't necessary for code to know the names of vars or ivars.

Eval can be used sometimes:

aList = (("a",val1),("b",val2),("c",val3))
...
for name,val in aList:
  eval("%s=%s" % (name,str(val)))

This only works for string and numeric values where str(val)==repr(val),
sort of.

Anyway, is there any way to get the same effect without using eval?  setattr
might be useful, and I haven't figured out how.

Thanks,

Edward
--------------------------------------------------------------------
Edward K. Ream   email:  edreamleo at charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list