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 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, inside the file 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 available to I cannot use

from __fileA__ import *

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

fileA calls functions from fileB.

Functions in fileB call functions from accesses and returns to

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

Expert Mod 2.5K+
P: 2,851
You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. #
  3. import FileB
  5. a = 12
  6. b = 24
  7. c = 24
  9. for attr in ('a', 'b', 'c'):
  10.     setattr(FileB, attr, eval(attr))
  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
  2. class Class_C:
  3.    test_string = "abc"
  4.    test_number = 1
  6. ## fileA
  7. import classC
  8. import fileB
  10. print classC.Class_C.test_string
  11. classC.Class_C.test_number = 2
  12. fileB.print_number()
  14. ## fileB
  15. import classC
  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.