By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

How do I call a user defined variable from another module... :P

Jory R Ferrell
P: 62
I would like to assign a variable to x, in this case a list.

A = ['a', 'b', 'c']
B = ['d', 'e', 'f']## list A and B would both be in a separate

x = A

print(importedModule.x)

When I run it this way, the program just throws an error because x is technically still undefined in the main module, so this is psuedocode...any ideas?
I would also like to know if the same solution to this can be used to call random/user-defined FUNCTIONS from an imported module instead of simple variables.
Oct 28 '11 #1

✓ answered by dwblas

Is there no way to get the two into the same block of memory?
No. There is no way to automatically map keyboard input to a variable in memory. You have to program it yourself, as this is programming the computer. You can use a dictionary to simplify things. An example follows.
Expand|Select|Wrap|Line Numbers
  1. ##---------- listModule  ----------
  2. external_dict = {"A":["a", "b", "c"],
  3.                  "B":["B", "B", "b"],
  4.                  "C":[1, 2, 3] }
  5.  
  6.  
  7.  ##---------- calling program  ----------
  8.  import listModule
  9.  
  10.  x = input('Type A, B, or C--> ')
  11.  x = x.upper()
  12.  
  13. if x in listModule.external_dict:
  14.     print(listModule.external_dict[x])
  15.  
  16. # or
  17. choices_dict = {"A":listModule.A,
  18. "                B":listModule.B }  ## etc but that would require thousands of hand built entries 
It is next to impossible to respond with good options shooting in the dark. Are the lists named in any convenient way that could be automatically placed into a dictionary? Can you program a replacement to listModule to generate lists are already in a dictionary? Can you iterate over the program's statements themselves and extract the lists into a dictionary?

Share this Question
Share on Google+
9 Replies


Expert 100+
P: 621
"A" and "x" both point to the same object so there is no difference, i.e. use "A".
Expand|Select|Wrap|Line Numbers
  1. A = ['a', 'b', 'c']
  2.  
  3. x = A
  4. print id(A)
  5. print id(x)
  6. x[1] = "1"
  7. print A 
When I run it this way, the program just throws an error because x is technically still undefined
Works fine for me, so post your code.
I would also like to know if the same solution to this can be used to call random/user-defined FUNCTIONS from an imported module instead of simple variables.
Try it for yourself and see.
Oct 28 '11 #2

Jory R Ferrell
P: 62
@dwblas
-------------------------------------------------
import listModule

x = input('Type A, B, or C.') ## either A, B, or C which are stored lists in imported module

print(listModule.x)

when run this way, importing the lists and their list names, I get an error saying that x is not defined.

>>>
Type A, B, or C.A
Traceback (most recent call last):
File "C:/Users/Normal Account/Desktop/bbpowre3vrjh", line 5, in <module>
print(listModule.x)
AttributeError: 'module' object has no attribute 'x'
>>>

I did write the first example improperly, sorry.
Oct 28 '11 #3

Expert 100+
P: 621
"A", as input by the user, is a string and is in an entirely different block of memory from the variable, "A" in listModule.
Expand|Select|Wrap|Line Numbers
  1. ##---------- listModule  ----------
  2. A = ["a", "c", "b"]
  3. B = "ABC"
  4. C = 123
  5.  
  6.  
  7. ##---------- calling program  ----------
  8. import listModule
  9.  
  10. x = input('Type A, B, or C--> ')
  11. x = x.upper()
  12.  
  13. if "A" == x:     ## note the comparison...string==x
  14.    print(listModule.A)  ## not a string (in quotes), but a variable
  15. elif "B" == x:
  16.    print(listModule.B)
  17. elif "C" == x:
  18.    print(listModule.C)
  19.  
  20. # if you have a lot of comparisons you can use a list or tuple or dictionary
  21. print("----- second way")
  22. for check_it in (("A", listModule.A), ("B", listModule.B), ("C", listModule.C)):
  23.     if x = check_it[0]:
  24.         print(check_it[1]) 
Oct 28 '11 #4

Jory R Ferrell
P: 62
@dwblas
---------------------------------------------
I know how to call a set number of functions or variables from an imported module....what I don't know how to do is
compactly allow a user to access over 10,000 (yes....ten thousand...)lists from an imported module(it'll actually be several modules :P). I'd need to write 10k elif blocks.

I can't simply write out every single elif block.
Well...I can but I really don't effing feel like it. lol :D
Besides, it would just be an impractically large file.
I want this to have the lowest footprint possible.
Is there no way to get the two into the same block of memory?
Oct 28 '11 #5

Expert 100+
P: 621
Is there no way to get the two into the same block of memory?
No. There is no way to automatically map keyboard input to a variable in memory. You have to program it yourself, as this is programming the computer. You can use a dictionary to simplify things. An example follows.
Expand|Select|Wrap|Line Numbers
  1. ##---------- listModule  ----------
  2. external_dict = {"A":["a", "b", "c"],
  3.                  "B":["B", "B", "b"],
  4.                  "C":[1, 2, 3] }
  5.  
  6.  
  7.  ##---------- calling program  ----------
  8.  import listModule
  9.  
  10.  x = input('Type A, B, or C--> ')
  11.  x = x.upper()
  12.  
  13. if x in listModule.external_dict:
  14.     print(listModule.external_dict[x])
  15.  
  16. # or
  17. choices_dict = {"A":listModule.A,
  18. "                B":listModule.B }  ## etc but that would require thousands of hand built entries 
It is next to impossible to respond with good options shooting in the dark. Are the lists named in any convenient way that could be automatically placed into a dictionary? Can you program a replacement to listModule to generate lists are already in a dictionary? Can you iterate over the program's statements themselves and extract the lists into a dictionary?
Oct 28 '11 #6

Jory R Ferrell
P: 62
@dwblas
----------------------------------------------------

Wow....ok.....this problem has held my project for over 2-3 months now....I was getting seriously discouraged and thinking I MIGHT be retarded. lol
That explains why compact test engines with with 10k questions in "addressed", sectioned modules aren't common. :P Thanks for your help.
Oct 28 '11 #7

Jory R Ferrell
P: 62
@dwblas
-----------------------------------------------

Copying the list to a dictionary should work. Again...ty.
Oct 28 '11 #8

P: 7
python is not PHP: there is no variable variables.

Normally, the data should be a dictionary saved using pickle. You should not do import hundreds/thousands of lists/tuples.

Here's a solution. I wrote your data as they are (very badly formatted) in an external file.

data.txt:
A = ['a', 'b', 'c']
B = ['d', 'e', 'f']
...
Expand|Select|Wrap|Line Numbers
  1. import re
  2. f = open('data.txt', 'r')
  3. data = {}
  4. for line in f:
  5.     key, value = re.findall('([^=]+)=(.*)', line)[0]
  6.     data[key.strip()] = eval(value.strip())
  7. f.close()
  8. ## from here, you can save the dictionary with pickle,
  9. ## ready to be loaded and quickly accessed
  10. x = raw_input('Type A, B, or C--> ')
  11. print data[x.upper()][1]  # returns 'e' if x == 'b'
  12. del data
  13.  
Avoid using eval().
Nov 14 '11 #9

Jory R Ferrell
P: 62
Hey...I already wrote the program...I had the answer a while ago....thanks though.
Nov 26 '11 #10

Post your reply

Sign in to post your reply or Sign up for a free account.