Does the child class not recognize the import of modules of the parent class?
I have two classes in two different modules:
animal.py
monkey.py
animal.py:
import json class Animal(object): pass
a monkey:
import animal class Monkey(animal.Animal): def __init__(self): super(Monkey, self).__init__() # Do some json stuff...
When I try to instantiate a Monkey
, I get
NameError: global name 'json' is not defined
But I import json
into the superclass definition module, so why not load it?
+4
Yarin
source share2 answers
It loads, but its name is not available in monkey.py
.
You can type animal.json
to get it (but why do you need it), or just type
import json
at monkey.py
. Python ensures that the module does not load twice.
+11
Thomas
source shareWell, importing python doesn't work as a C # include pre-processor directive. They only import the module into the namespace of the import module, and not into the global namespace. So, you have to import json into every module that you are going to use.
+2
smichak
source share