0

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.

2 Answers 2

4

That's not really what variable names are meant for. The numbers 1 and 2 in tuple1 and tuple2 either mean something (e.g. the 1 really means "1 person" and the 2 "2 people") or they don't, they're only placeholders.

If they do mean something, you should probably use a dictionary:

>>> my_data = {1: (1,2,3), 2: (4,5,6)}
>>> my_data
{1: (1, 2, 3), 2: (4, 5, 6)}
>>> my_data[1]
(1, 2, 3)
>>> my_data[2]
(4, 5, 6)

if they don't, and you simply have two tuples, then you might as well use a list:

>>> my_data = [(1,2,3), (4,5,6)]
>>> my_data[0]
(1, 2, 3)
>>> my_data[1]
(4, 5, 6)
>>> for tup in my_data:
...     print tup
... 
(1, 2, 3)
(4, 5, 6)

You definitely don't need to use eval.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Thanks for reply, each tuple holds data for a specific case and there are about 150 cases containing 15 elements. Each case holds data that never changes so I used the tuple as it was immutable and wanted to protect the data. I have just tested with a dictionary and it works well, if you think this is a better way I will change my code. Also is it indeed better to import data using 'import data' rather than 'from data import *' in this case. If you could expand more on not needing to use eval() it would help my understanding greatly. Many thanks again, SNIFFY
0

Your method declaration for string() is not in the data file so data.string() would not work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.