2013-03-25 3 views
3

아래 그림과 같이 부모 클래스와 자식 클래스를 만들면 부모 클래스의 인수가 자동으로 자식 클래스에서 가져 오지 않는 이유는 무엇입니까?이유는 파이썬 클래스/하위 클래스 상속 뒤에

class testParent(object): 
    def __init__(self,testParentParam1,testParentParam2): 
     pass 


class testChild(testParent): 
    def __init__(self,testParentParam1,testParentParam2,testChildParam1,testChildParam2): 
     pass 

이 코드보다 더 나은가요 ...

class testParent(object): 
    def __init__(self,testParentParam1,testParentParam2): 
     pass 


class testChild(testParent): 
    def __init__(self,testChildParam1,testChildParam2): 
     pass 
+2

생성자가 비어있는 동안 어떤 것이 더 좋은지 알기 어렵습니다. – bereal

+0

두 가지 모두 작동 할 수 있으며, 수행하려는 작업에 따라 다릅니다. 문제가있는 곳을 파악할 수있는 충분한 정보가 없습니다. – cmd

+0

필자가 작성한 (필자 자신의 '__init__'을 필요로하는) ​​하위 클래스는 수퍼 클래스와 동일한 인수를 받아 들여 수정되지 않은 채로 전달했습니다. 대다수는 더 적은 인수 또는 다른 인수를 수용했습니다 (그리고 어떻게 든 수퍼 클래스에 전달할 인수를 계산했습니다). – delnan

답변

4

파생 클래스가 기반을 확장 ...

나는 명시 적 낫다는 것을 이해하지만, 나는 어떤 상황이 코드에서 궁금하네요 수업. 그것은 그들이 확장을하기 위해 건설 시간에 더 많거나 적은/다른 정보를 필요로 할 수 있음을 의미합니다. 다음을 고려하십시오.

class BaseTextDocument(object): 
    def __init__(self, content): 
     self.content = content 

class WordDocument(object): 
    def __init__(self, path, word_version="guess_from_file"): 
     content = parse_word_document(path, word_version) 
     super(WordDocument, self).__init__(content)