[Tutor] Dynamically assign variable names to tuple objects
Martin A. Brown
martin at linux-ip.net
Tue Mar 1 19:44:51 CET 2011
Sean,
: 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. This is as far as I've gotten:
:
: <code>
: import sys
:
: myfiles = tuple(sys.argv[1:])
: numfiles = len(myfiles)
:
: def makeTuple(file):
: outlist = []
: f = open(file, 'r')
: for line in f.readlines():
: line = line.rstrip(' \n')
: outlist.append(line)
: return tuple(outlist)
:
: for i in range(numfiles):
: makeTuple(myfiles[i])
: </code>
:
: The script creates the tuples as it was intended to, but I'm not sure
: how to assign variable names to each tuple so that I can work with
: them afterwards. How would you dynamically assign variable names to
: each tuple created by makeTuple(), so that they can be manipulated
: further?
Well, your function makeTuple() is returning something. You could
simply take the returned result and assign that to whatever variable
you wish.
Some minor notes:
* Your use of numFiles, i.e. 'for i in range(numFiles):' is
not necessary, you can iterate directly over the file list.
* You are using 'f.readlines()' which loads the whole file into
memory (not that this is important in this example, since you
are reading the whole file into memory, anyway). Nonetheless,
one wonderfully pythonic feature is that you can iterate
directly over File-like objects. Try this:
file = open(filename,'r')
for line in file:
# -- do something with each line
* It seems a bit strange to me that you want to use a tuple for
the contents of these files. A list of lines in a file seems
more natural (to me). Tuples are hashable, which is quite
convenient, but you can't modify them.
* You use a tuple also for the list of files the user has passed.
Why not just use the list you already have? sys.argv
* There are different camps on the creation of lists and
dictionaries. I find the English-like creation easier to read,
so I always do:
d = dict() # -- same as d = {}
l = list() # -- same as l = []
Do you know how to use a dictionary? It seems a natural for what it
looks like you are trying to do (although you have not explained
your intent, so can only guess).
d = dict()
for file in sys.argv[1:]:
d[file] = makeTuple(file)
You then sent this:
: for i in range(numfiles):
: varlist.append('tuple'+str(i))
: vars()[varlist[i]] = makeTuple(myfiles[i])
I saw in your follow-up that you went straight for vars(). I really
don't think that's what you wish to use. Get rid of vars(), he had
to go to jail. Don't go visit vars() again for at least two months,
then maybe he'll be out on probation.
Adjusting your code (instead of the dictionary approach I suggested
above), I'd suggest using your list variable directly!
varlist = []
for i in myfiles:
varlist.append( makeTuple( i ) )
Good luck,
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list