Python에서 상속을 사용하면 재미있는 가져 오기 오류가 발생합니다.Python 하위 모듈을 가져온 부모 모듈을 사용할 수 없습니다.
상위 클래스에서 sqlite3 모듈을 가져옵니다. 하위 클래스에서 sqlite3 함수를 사용하려고했지만 "NameError : 전역 이름 'sqlite3'이 정의되지 않았습니다."라는 오류가 발생합니다. 왜 이런 일이 발생합니까? & 어떻게 수정합니까?
2 개 개의 클래스는 별도의 파일에 있습니다
Parent.py
import sqlite3
class Parent:
def __init__(self):
self.create_database()
def create_database(self):
""" Virtual function to be overriden in child classes """
pass
...more class functions that use sqlite3 functions
Child.py
import Parent
class Child(Parent.Parent):
def create_database(self):
self.db = sqlite3.connect("test.db") # Error occurs HERE
c = Child()
단순히 자식 모듈에서도'import sqlite3'을 사용하십시오. – jsbueno
왜 부모님을 통해 sqlite3에 액세스 하시겠습니까? 또한 "import *"는 그리 좋지 않습니다. –
동의 함,이 것은 받아 들일 수있는 대답이 아닙니다. "import sqlite3"이 올바른 방법입니다. –