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 18:20:45 +0200- injecting 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.)Mon, 12 Aug 2013 17:39:10 +0200https://ask.sagemath.org/question/10423/injecting-names-into-global-namespace-doesnt-work-with-doctest/
- Answer by Nicolas M ThiĆ©ry for <p>Here is a minimal example of the problem I am running into. I have a file "MyClass.py":</p>
<pre><code>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)
</code></pre>
<p>If I doctest it, I get <code>NameError: name 'A0' is not defined</code>, 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.</p>
<p>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.)</p>
https://ask.sagemath.org/question/10423/injecting-names-into-global-namespace-doesnt-work-with-doctest/?answer=15336#post-id-15336`sage.misc.misc.inject_variable` might be your friend.Mon, 12 Aug 2013 17:53:04 +0200https://ask.sagemath.org/question/10423/injecting-names-into-global-namespace-doesnt-work-with-doctest/?answer=15336#post-id-15336
- Comment by paragon for <p><code>sage.misc.misc.inject_variable</code> might be your friend.</p>
https://ask.sagemath.org/question/10423/injecting-names-into-global-namespace-doesnt-work-with-doctest/?comment=17167#post-id-17167Thank you! It is my friend!Mon, 12 Aug 2013 18:20:45 +0200https://ask.sagemath.org/question/10423/injecting-names-into-global-namespace-doesnt-work-with-doctest/?comment=17167#post-id-17167