I have two Python files:
b.py
:
def buzz():
foobar = Foobar()
c.py
:
from b import buzz
class Foobar:
pass
buzz()
Running python c.py
raises:
NameError: name 'Foobar' is not defined
Looks like there is a basic Python's import mechanism I still don't understand. I would expect that, when buzz()
is called, it has dynamically access to the environment now containing Foobar
.
Of course (?), if I replace the importation of buzz
by its definition, it works:
d.py
:
def buzz():
foobar = Foobar()
class Foobar:
pass
buzz()
Context.
This may be an XY-problem. Ultimately, I want to be able to change the behaviour of buzz
depending on which Foobar
variant has previously been imported. However, I would be interested in understanding why b.py
/ c.py
fails.