[Chicago] Using globals across files
Chris Cope
chris at copester.com
Mon Mar 27 18:39:49 CEST 2006
You're right, having MyTests reference tests.testsRun is probably the
better approach anyway. In 4 years of programming with python, I've
never used globals before, and had no idea seamlessly sharing them
between files, like with an external in C, wasn't feasible. :)
Thanks for the help,
Chris
Brant Harris wrote:
> Globals aren't exactly "globals" like you probably think of them.
> Really a global is just a module-level variable. There is no
> construct for a totally global variable. But that's okay, because I
> don't think you really want that. What you probably want is a class
> that will store that info. But perhaps you just want something
> quicker, or just easier. In that case, declare and initiate to 0 the
> variables "testsPassed" and "testsRun" in "tests.py", instead of in
> MyTests.py. Then after you "import tests", you can reference them as
> "tests.testsPassed" and "tests.testsRun". So to the imported module
> the variables are "globals", but to the code that imports that module
> (MyTests.py in this case) the variables are properties of the "tests"
> module.
>
> On 3/26/06, Chris Cope <chris at copester.com> wrote:
>
>>I'd like to set global varibles and use them across files, but can't
>>find out how to do this (if it's possible). I'm using it for keeping
>>track of unit tests, and believe a global is the right solution, rather
>>than something like passing testsPassed and testsRun to Test(). Google
>>doesnt return a solution, just other people with the same problem. Any
>>ideas?
>>
>>I get the error "NameError: global name 'testsPassed' is not defined"
>>when trying to increment it in tests.py. The code follows.
>>
>>Thanks,
>>Chris
>>
>># File tests.py
>>import inspect
>>
>>def Test(cond):
>> global testsPassed
>> global testsRun
>> if cond:
>> testsPassed += 1
>> testsRun += 1
>>
>>
>>
>># File MyTests.py
>>
>>global testsRun
>>global testsPassed
>>testsRun = 0
>>testsPassed = 0
>>
>>import tests
>>
>>def runTest1():
>> x = 0
>> y = 0
>> tests.Test(x == y)
>>
>>runTest1()
>>print "Summary:", testsPassed, "of", testsRun, "test(s) passed."
>>_______________________________________________
>>Chicago mailing list
>>Chicago at python.org
>>http://mail.python.org/mailman/listinfo/chicago
>>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
More information about the Chicago
mailing list