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

"extern variable" in PYTHON:

P: n/a
I have a file fileA.py where my students input some data and fill in a certain portion of the code. There are a few functions they call from a module, fileB.py inside the file fileA.py. When they are calling functions, I do not want them to pass data as arguments , since that would confuse them a lot (they are 9th grade students). So, I want to make the data of fileA.py available to fileB.py. I cannot use

from __fileA__ import *

because, that will be a recursive call. I have already used it in fileA.py. So, I need a way out of this, where I can use variable defined in fileA.py to be used by fileB.py. I had an idea of creating a bypass.py such that

fileA calls functions from fileB.

Functions in fileB call functions from bypass.py

bypass.py accesses fileA.py and returns to fileB.py.

So, the recursion is kind of avoided.
Nov 3 '10 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. # FileA.py
  2.  
  3. import FileB
  4.  
  5. a = 12
  6. b = 24
  7. c = 24
  8.  
  9. for attr in ('a', 'b', 'c'):
  10.     setattr(FileB, attr, eval(attr))
  11.  
  12. result = FileB.f()
Nov 3 '10 #2

Expert 100+
P: 624
They should be able to understand writing the data to a file and closing the file. The function in fileB would open and read the file. Also, you can use a class attribute, but that is possibly beyond them as well.
Expand|Select|Wrap|Line Numbers
  1. ## file classC.py
  2. class Class_C:
  3.    test_string = "abc"
  4.    test_number = 1
  5.  
  6. ## fileA
  7. import classC
  8. import fileB
  9.  
  10. print classC.Class_C.test_string
  11. classC.Class_C.test_number = 2
  12. fileB.print_number()
  13.  
  14. ## fileB
  15. import classC
  16.  
  17. def print_number():
  18.     print "inside fileB", classC.Class_C.test_number 
Nov 3 '10 #3

Post your reply

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