복붙노트

[PYTHON] super ()가 오류와 함께 실패합니다. TypeError 부모가 객체에서 상속하지 않는 경우 "인수 1은 classobj가 아닌 유형이어야합니다"

PYTHON

super ()가 오류와 함께 실패합니다. TypeError 부모가 객체에서 상속하지 않는 경우 "인수 1은 classobj가 아닌 유형이어야합니다"

알아낼 수없는 오류가 발생합니다. 내 샘플 코드에 어떤 문제가 있는지 실마리가 있습니까?

class B:
    def meth(self, arg):
        print arg

class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)

print C().meth(1)

나는 'super'내장 메소드의 도움으로 샘플 테스트 코드를 얻었습니다. "C"클래스는

다음은 오류입니다.

Traceback (most recent call last):
  File "./test.py", line 10, in ?
    print C().meth(1)
  File "./test.py", line 8, in meth
    super(C, self).meth(arg)
TypeError: super() argument 1 must be type, not classobj

참고로, 여기 파이썬 자체의 도움 (슈퍼)이 있습니다 :

Help on class super in module __builtin__:

class super(object)
 |  super(type) -> unbound super object
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type, type2) -> bound super object; requires issubclass(type2, type)
 |  Typical use to call a cooperative superclass method:
 |  class C(B):
 |      def meth(self, arg):
 |          super(C, self).meth(arg)
 |

해결법

  1. ==============================

    1.귀하의 문제는 클래스 B가 "새로운 스타일"클래스로 선언되지 않는다는 것입니다. 다음과 같이 변경하십시오.

    귀하의 문제는 클래스 B가 "새로운 스타일"클래스로 선언되지 않는다는 것입니다. 다음과 같이 변경하십시오.

    class B(object):
    

    작동 할 것입니다.

    super () 및 모든 하위 클래스 / 수퍼 클래스 항목은 새 스타일 클래스에서만 작동합니다. 모든 클래스 정의에 항상 그 (객체)를 입력하는 습관에 빠져서 새로운 스타일의 클래스인지 확인하는 것이 좋습니다.

    구식 클래스 ( "클래식"클래스라고도 함)는 항상 classobj 유형입니다. 새로운 스타일의 클래스는 유형 유형입니다. 이것이 본 오류 메시지입니다.

    TypeError : super () 인수 1은 classobj가 아닌 type이어야합니다.

    직접보십시오.

    class OldStyle:
        pass
    
    class NewStyle(object):
        pass
    
    print type(OldStyle)  # prints: <type 'classobj'>
    
    print type(NewStyle) # prints <type 'type'>
    

    Python 3.x에서는 모든 클래스가 새로운 스타일임을 유의하십시오. 구식 클래스의 구문을 계속 사용할 수 있지만 새로운 스타일의 클래스를 얻을 수 있습니다. 따라서 파이썬 3.x에서는이 문제가 발생하지 않습니다.

  2. ==============================

    2.또한 클래스 B를 변경할 수없는 경우 다중 상속을 사용하여 오류를 수정할 수 있습니다.

    또한 클래스 B를 변경할 수없는 경우 다중 상속을 사용하여 오류를 수정할 수 있습니다.

    class B:
        def meth(self, arg):
            print arg
    
    class C(B, object):
        def meth(self, arg):
            super(C, self).meth(arg)
    
    print C().meth(1)
    
  3. ==============================

    3.파이썬 버전이 3.X이면 괜찮습니다.

    파이썬 버전이 3.X이면 괜찮습니다.

    파이썬 버전이 2.X이고,이 코드를 추가 할 때 슈퍼가 작동한다고 생각합니다.

    __metaclass__ = type
    

    그래서 코드는

    __metaclass__ = type
    class B:
        def meth(self, arg):
            print arg
    class C(B):
        def meth(self, arg):
            super(C, self).meth(arg)
    print C().meth(1)
    
  4. ==============================

    4.또한 Python 2.7을 사용할 때 게시 된 문제에 직면했습니다. 그것은 파이썬 3.4와 아주 잘 작동하고있다.

    또한 Python 2.7을 사용할 때 게시 된 문제에 직면했습니다. 그것은 파이썬 3.4와 아주 잘 작동하고있다.

    Python 2.7에서 작동하도록하려면 프로그램 상단에 __metaclass__ = type 특성을 추가했습니다.

    __metaclass__ : 이전 스타일의 클래스와 새로운 스타일의 클래스에서 쉽게 전환 할 수 있습니다.

  5. from https://stackoverflow.com/questions/1713038/super-fails-with-error-typeerror-argument-1-must-be-type-not-classobj-when by cc-by-sa and MIT license