A
이 작성 될 때 이름이 아직 알려지지 않은 클래스 B
을 상속해야하는 클래스 A
이 있습니다. A
이 상속되지 않는다고 선언 할 수 있으며 A
의 인스턴스 생성 중에 B
을 기본 클래스로 추가 할 수 있습니까? 예 :Python에서 동적으로 상속을 설정합니다.
먼저 파일
class B:
def __init__(self):
self.__name = "Class B"
def name(self):
return self.__name
두 번째 파일
class A:
def __init__(self):
self.__name = "Class A"
# At some point here, the appropriate module name and location is discovered
import sys
sys.path.append(CustomModulePath)
B = __import__(CustomModuleName)
magic(A, B) # TODO What should magic() do?
a = A()
print a.name() # This will now print "Class A", since name() is defined in B.
왜 이렇게해야하는지 설명 할 수 있습니까? – danben
이 응용 프로그램에서는 일반 클래스 이름이 아닌 응용 프로그램 별 메타 데이터를 기반으로 클래스를 조작해야합니다. – dpq