In my current working directory I have the files active.ipynb which contains class B, subclass of A, and I have A.py where A is defined.
When I'm defining everything within the notebook (i.e. class A and B are both defined within a cell in the notebook), everything works fine which is what I expect. When class A is moved into it's own file, I get problems. Importing math in class B does not help, and I also tested this in a separate notebook from my real project to make sure I'm not doing something detrimental somewhere.
Consider
A.py contents:
#edited to reflect this issue occurring regardless of import location
import math
class A:
def method(self):
#parent related code...
self.parentmethod()
def parentmethod(self):
print(math.pi)
In my current cell I have:
import A
class B(A.A):
def method(self):
#child related overridden code...
self.parentmethod()
b = B()
b.method()
and I get the following error:
NameError Traceback (most recent call last)
<ipython-input-84-42c1e6969f03> in <module>
9 b = B()
10
---> 11 b.method()
<ipython-input-84-42c1e6969f03> in method(self)
5 def method(self):
6 #child related code...
----> 7 self.parentmethod()
8
9 b = B()
~\Documents\Projects\FunctionNetwork\A.py in parentmethod(self)
7
8 def parentmethod(self):
----> 9 print(math.pi)
NameError: name 'math' is not defined
What's going on?
I figured it out by accidentally repeating the error moments ago, and now I can replicate it at will.
I copied class A from JN, and accidentally missed the math import at the module level. I then imported class A into a fresh cell and called it's method which lead to a name error referencing the math module. This is expected because I didn't import math anywhere.
I added the math import to A.py, saved it, then reran the cell assuming it would reimport A.py with the new information. This is not the case. When I cleared the notebook, only then did it recognize that I updated A.py.
Apparently, being consistent can be a boon even if you're consistently sloppy.