ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 12 Aug 2013 17:39:10 +0200injecting names into global namespace doesn't work with doctesthttps://ask.sagemath.org/question/10423/injecting-names-into-global-namespace-doesnt-work-with-doctest/Here is a minimal example of the problem I am running into. I have a file "MyClass.py":
class MyClass(object):
def __init__(self,subscript):
self.subscript = subscript
def __repr__(self):
return "MyClass " + str(self.subscript)
def make_MyClass(n):
"""
Creates n MyClass instances and assigns them to to variables A0, ..., A(n-1).
Examples::
sage: make_MyClass(3)
sage: A0
MyClass 0
sage: A2
MyClass 2
sage: A1.subscript
1
"""
for i in range(n):
globals()["A" + str(i)] = MyClass(i)
If I doctest it, I get `NameError: name 'A0' is not defined`, but if I just load the file and type in the commands, it works how I want it to. It must be something about how globals() interacts with doctest.
I know it is possible to make this work, because for example the function var does something like this. I tried looking at the var.pyx source, but it looks like they are doing the same thing as me. (There is a comment about globals() being the reason that it had to be Cython. I tried making the example above a pyx, but that didn't seem to help.)paragonMon, 12 Aug 2013 17:39:10 +0200https://ask.sagemath.org/question/10423/