zoukankan      html  css  js  c++  java
  • super方法的使用问题

    class A:
        def __new__(cls, name):
            print("A.__new__called")
            return super().__new__(cls,name)
        def __init__(self,name):
            self.name = name
            print ("A.__init__ called")
    
        def __str__(self):
            return self.name
    s= A("xxx")
    print(s)
    
    
    class B:
        def __new__(cls, *args, **kwargs):
            print("B.__new__ called")

    错误信息

    Traceback (most recent call last):
    .....
        return super().__new__(cls.name)
    AttributeError: type object 'A' has no attribute 'name'
    A.__new__called

    新式类与旧式类 super继承方式不同

    更改方法

    class A:
        def __new__(cls,name):
            print("A.__new__called")
            return super().__new__(cls)
        def __init__(self,name):
            self.name = name
            print ("A.__init__ called")
    
        def __str__(self):
            return self.name
    s= A("xxx")
    print(s)
    
    
    class B:
        def __new__(cls, *args, **kwargs):
            print("B.__new__ called")
  • 相关阅读:
    Wannafly 挑战赛12 E
    HIT ACM 2018春 week2 codeforces.com/gym/101652 题解
    Hihocoder [Offer收割]编程练习赛49 题目4 : 第K小先序遍历
    HDU
    ZOJ
    HYSBZ
    POJ
    HYSBZ
    POJ 2796 Feel Good 题解
    逆元基本知识
  • 原文地址:https://www.cnblogs.com/wangxishan/p/6596502.html
Copyright © 2011-2022 走看看