Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何添加到从另一个类继承的python类的初始定义中?_Python_Class_Inheritance_Instance - Fatal编程技术网

如何添加到从另一个类继承的python类的初始定义中?

如何添加到从另一个类继承的python类的初始定义中?,python,class,inheritance,instance,Python,Class,Inheritance,Instance,我试图在继承自类的类中定义self.data class Object(): def __init__(self): self.data="1234" class New_Object(Object): # Code changing self.data here 但我遇到了一个问题 class Object(): def __init__(self): self.data="1234" 我这里有一个从别处导入的开始类,假设这个类是一

我试图在继承自类的类中定义self.data

class Object():
    def __init__(self):
        self.data="1234"

class New_Object(Object):
    # Code changing self.data here
但我遇到了一个问题

class Object():
    def __init__(self):
        self.data="1234"
我这里有一个从别处导入的开始类,假设这个类是一个通用类,所以我根本不能修改原始类

在原始版本中,该实例在类中称为self,在定义_uinit_;中定义为self

因此,如果我想从类对象继承,但在新对象中定义self.data,我想我必须在新对象中定义uuu init uuu,但这会覆盖来自新对象的uuu init uu

有没有办法不用从对象复制粘贴uu init uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

class New_Object(Object):
    def __init__(self):
        super(NewObject, self).__init__()
        self.info = 'whatever'
您可以使用super调用原始实现

class New_Object(Object):
    def __init__(self):
        super(NewObject, self).__init__()
        self.info = 'whatever'
这就是为什么:

这就是为什么:

您可以使用super.\uuuu init\uuuuuu调用对象。\uuuuu init\uuuuu来自新对象。\uuuuu init\uuuuu

你会做什么:

class Object:
    def __init__(self):
        print("Object init")
        self.data = "1234"

class New_Object(Object):
    def __init__(self):
        print("calling super")
        super().__init__()
        print("data is now", self.data)
        self.data = self.data.split("3")

o = New_Object()

# calling super
# Object init
# data is now 1234
请注意,只要您使用的是Python 3,就不必给super提供任何参数。

您可以使用super.\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

你会做什么:

class Object:
    def __init__(self):
        print("Object init")
        self.data = "1234"

class New_Object(Object):
    def __init__(self):
        print("calling super")
        super().__init__()
        print("data is now", self.data)
        self.data = self.data.split("3")

o = New_Object()

# calling super
# Object init
# data is now 1234

请注意,只要您使用的是Python 3,就不必为super提供任何参数。

答案是在子类的uu init uu_u期间显式调用超类的u init uu。这可以通过以下两种方式之一实现:

Object.__init__(self)   # requires you to name the superclass explicitly

后者还要求您确保使用新样式的类:在Python3中总是这样,但在Python2中,您必须确保从内置对象类继承。在Python 3中,它实际上可以更简单地表达:

super().__init__()
就我个人而言,在我的大多数代码中,必须显式命名超类的缺点一点也不不利,而Object.\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。这是因为我的大部分代码都是单继承的。当您有多重继承时,超级路由就有了自己的功能。看

Python 2示例:

class Object(object):
    def __init__(self):
        self.data = "1234"

class NewObject:
    def __init__(self):
        # subclass-specific stuff
        super(NewObject, self).__init__()
        # more subclass-specific stuff

答案是在子类的uuu init_uuu期间显式调用超类的uuu init_uuu。这可以通过以下两种方式之一实现:

Object.__init__(self)   # requires you to name the superclass explicitly

后者还要求您确保使用新样式的类:在Python3中总是这样,但在Python2中,您必须确保从内置对象类继承。在Python 3中,它实际上可以更简单地表达:

super().__init__()
就我个人而言,在我的大多数代码中,必须显式命名超类的缺点一点也不不利,而Object.\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。这是因为我的大部分代码都是单继承的。当您有多重继承时,超级路由就有了自己的功能。看

Python 2示例:

class Object(object):
    def __init__(self):
        self.data = "1234"

class NewObject:
    def __init__(self):
        # subclass-specific stuff
        super(NewObject, self).__init__()
        # more subclass-specific stuff

任何编辑都是有用的,我对术语和措辞的理解很糟糕。请注意,self是类的实例,而不是类本身。@MisterMiyagi啊,谢谢你让我知道。任何编辑都是有用的,我对术语和措辞的理解很糟糕。请注意,self是类的实例,不是课堂本身。@Mistermiagi啊,谢谢你让我知道。