Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:31:16+00:00 2026-06-02T16:31:16+00:00

In one file called say ‘data’ I have some tuples: tuple1 = (1, 2,

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:31:17+00:00Added an answer on June 2, 2026 at 4:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have file.php with three functions and an echo statement: function one() {
Let us say I have only one file in my project called test.c; the
Let's say there is a file called myfile.txt with the following contents: one two
I have a file called webpart.cs which is called by one of my masterpages
I have one file (for example: test.txt), this file contains some lines and for
Let's say I have a file called example.py with the following content: def func_one(a):
lets say i have 5 lines within a txt file called users.txt each line
Say I have an object called data which contains a variety of information. Let's
I have a file called function.js which has all my jQuery for my aplication
Lets say I have a file called foo.txt encoded in utf8: aoeu qjkx ñpyf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.