In one file called say 'data' I have some tuples:
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
In another file, I import the data file and have a function that concatenates strings to be able to call these tuples.
import data
def string(tupleNumber):
return 'tuple' + str(tupleNumber)
If I try and print the tuple, I get an error as string(1) is not defined in data.
print data.tuple1 # This works
print string(1) # This also works
print data.string(1) # This does not
The following result occurs:
(1, 2, 3)
tuple1
Traceback (most recent call last):
File "C:/Python27/Scripts/tuplecall.py", line 10, in <module>
print data.string(1)
AttributeError: 'module' object has no attribute 'string'
I can see why it's not working but can't think how to change string(1) to look like tuple1 when I try to call the data set.
The way I have got round this so far is to use the following code:
from data import *
def string(tupleNumber):
return 'tuple' + str(tupleNumber)
print tuple1
print string(1)
print eval(string(1))
Which gives the results:
(1, 2, 3)
tuple1
(1, 2, 3)
However as my 'data' file has much data in it I was concerened I was wasting memory loading in each tuple with 'from data import *' when I can just call it as in the first case when I need it, but I'm obvioulsy missing something vital.
Also I have seen that eval() is not the best thing to use but so far it's all I have found that works. So if anyone is able to give me some ideas on how to improve my understanding I would be very much appreciated.
Kind regards
SNIFFY.