[Tutor] Dynamically assign variable names to tuple objects

Sean Carolan scarolan at gmail.com
Tue Mar 1 19:10:49 CET 2011


On Tue, Mar 1, 2011 at 11:55 AM, Sean Carolan <scarolan at gmail.com> wrote:
> Maybe someone can help with this.  I have a function that takes a
> single file as an argument and outputs a tuple with each line of the
> file as a string element.  This is part of a script that is intended
> to concatenate lines in files, and output them to a different file.

Not sure if this is the "right" or best way to do this, but I ended up
using vars() to assign my variable names, like so:

import sys

myfiles = tuple(sys.argv[1:])
numfiles = len(myfiles)
varlist = []

def makeTuple(file):
   6 lines:    outlist = [] ----------

for i in range(numfiles):
    varlist.append('tuple'+str(i))
    vars()[varlist[i]] = makeTuple(myfiles[i])


More information about the Tutor mailing list