Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child class doesn't recognize module imports of parent class?

I've got two classes in two different modules:

  • animal.py
  • monkey.py

animal.py:

import json

class Animal(object):
    pass

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 a

NameError: global name 'json' is not defined

But I'm importing json in the defining module of the super class, so why wouldn't it be loaded?

like image 728
Yarin Avatar asked Dec 12 '22 02:12

Yarin


1 Answers

It is loaded, but its name is not available in the scope of monkey.py.

You could type animal.json to get at it (but why would you), or just type

import json

in monkey.py as well. Python will ensure that the module is not loaded twice.

like image 80
Thomas Avatar answered May 11 '23 14:05

Thomas