使用多重继承调用__init__ [英] Calling __init__ with multiple inheritance

查看:69
本文介绍了使用多重继承调用__init__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我正在使用新的(对象)类,并且正常使用callin super(...)来调用init的

motherclass,workes很好。


不,我有一个多个因素的案例,想要询问这是否是

调用init的正确和常见情况:


class母亲(对象):

def __init __(self,param_mother):print''母亲'

class Father (对象):

def __init __(self,param_father):打印''父亲''

class Child(母亲,父亲):

def __init __(self,param_mother,param_father):

母亲.__初始__(自我,param_mother)

父亲.__初始__(自我,param_mother)

孩子=孩子(1,2)


谢谢,AXEL。

Hello!

Im working with new (object) classes and normaly call init of ther
motherclass with callin super(...), workes fine.

No, I''ve got a case with multiple inherance and want to ask if this is
the right and common case to call init:

class Mother(object):
def __init__(self, param_mother): print ''Mother''
class Father(object):
def __init__(self, param_father): print ''Father''
class Child(Mother, Father):
def __init__(self, param_mother, param_father):
Mother.__init__(self, param_mother)
Father.__init__(self, param_mother)
child = Child(1, 2)

Thanks, AXEL.

推荐答案

Axel Straschil写道:
Axel Straschil wrote:
我正在使用新的(对象)类,并且使用callin super(...)正常调用init的
母类,工作很好。

不,我有一个多重因素的案例,想要询问这是否是调用init的正确和常见案例:

母亲(对象):
def __init __(self,param_mother):打印''母亲'
类父亲(对象):
def __init __(self,param_father):print''父亲''
班级孩子(母亲,父亲):
def __init __(self,param_mother,param_father):
母亲.__初始__(自我,param_mother)
父亲.__ init __(自我,param_mother)
child =孩子(1,2)
Im working with new (object) classes and normaly call init of ther
motherclass with callin super(...), workes fine.

No, I''ve got a case with multiple inherance and want to ask if this is
the right and common case to call init:

class Mother(object):
def __init__(self, param_mother): print ''Mother''
class Father(object):
def __init__(self, param_father): print ''Father''
class Child(Mother, Father):
def __init__(self, param_mother, param_father):
Mother.__init__(self, param_mother)
Father.__init__(self, param_mother)
child = Child(1, 2)




它看起来是正确的 - 至少它/看起来/正确,然后尝试引用它.. 。


这是一种替代方法,可以在多继承环境中按摩初始化程序签名

位以使用super():


class母亲(对象):

def __init __(self,p_mother,** more):

print" Mother",p_mother,更多

super(妈妈,自我).__ init __(p_mother = p_mother,**更多)


class父亲(对象):

def __init __(self,p_father,** more):

print" Father',p_father,more

super(父亲,自我).__ init __(p_father = p_father,** more)


class Child (母亲,父亲):

def __init __(自我,p_mother,p_father,**更多):

print" Child",p_father,p_mother,more

super(儿童,自我).__ init __(p_mother = p_mother,p_father = p_father,

** more)


儿童(1岁, 2)


无法识别的参数被隐藏在< more>中字典。


彼得



It looks correct -- at least it /looked/ correct before tried to quote it...

Here is an alternative approach that massages the initializer signatures a
bit to work with super() in a multiple-inheritance environment:

class Mother(object):
def __init__(self, p_mother, **more):
print "Mother", p_mother, more
super(Mother, self).__init__(p_mother=p_mother, **more)

class Father(object):
def __init__(self, p_father, **more):
print "Father", p_father, more
super(Father, self).__init__(p_father=p_father, **more)

class Child(Mother, Father):
def __init__(self, p_mother, p_father, **more):
print "Child", p_father, p_mother, more
super(Child, self).__init__(p_mother=p_mother, p_father=p_father,
**more)

Child(1, 2)

Unrecognized parameters are stashed away into the <more> dictionary.

Peter


如果我想调用其他方法怎么办?修改你的例子

位,我想调用Child的reset()方法来调用

母亲和父亲重置()方法,而不引用它们按名称,

ie,Mother.reset(个体经营)。


------------------ -

class母亲(对象):

def __init __(自我,p_mother,**更多):

print" Mother",p_mother ,更多

super(妈妈,自己).__ init __(p_mother = p_mother,**更多)

def reset(self):

打印''重置母亲'


类父亲(对象):

def __init __(自我,p_father,**更多):

print" Father',p_father,more

super(父亲,自我).__ init __(p_father = p_father,** more)

def reset(self):

打印''重置父亲'


class Child(母亲,父亲):

def __init __(self,p_mother, p_father,** more):

print" Child",p_mother,p_father,more

sup呃(孩子,自己).__ init __(p_mother = p_mother,

p_father = p_father,** more)

def reset(self):

打印''重置孩子''

#我想在这里调用Mother.reset()

#和Father.reset(),但是它不起作用。

打印''尝试'超级"''

super(儿童,自我).reset()

#接下来的两个有效,但需要参考

#母亲和父亲直接。

打印直接打电话

Mother.reset(个体经营)

Father.reset(个体经营)


a =儿童(1,2)

a。重置()

-------------------


谢谢,

Phil

What if I want to call other methods as well? Modifying your example a
bit, I''d like the reset() method call of Child to invoke both the
Mother and Father reset() methods, without referencing them by name,
i.e., Mother.reset(self).

-------------------
class Mother(object):
def __init__(self, p_mother, **more):
print "Mother", p_mother, more
super(Mother, self).__init__(p_mother=p_mother, **more)
def reset(self):
print ''resetting Mother''

class Father(object):
def __init__(self, p_father, **more):
print "Father", p_father, more
super(Father, self).__init__(p_father=p_father, **more)
def reset(self):
print ''resetting Father''

class Child(Mother, Father):
def __init__(self, p_mother, p_father, **more):
print "Child", p_mother, p_father, more
super(Child, self).__init__(p_mother=p_mother,
p_father=p_father, **more)
def reset(self):
print ''resetting Child''

# I would like to invoke both Mother.reset()
# and Father.reset() here, but it doesn''t work.
print ''trying "super"''
super(Child, self).reset()

# These next two do work, but require referencing
# Mother and Father directly.
print "calling directly"
Mother.reset(self)
Father.reset(self)

a = Child(1, 2)
a.reset()
-------------------

Thanks,
Phil


ph ***************** @ yahoo.com 写道:
ph*****************@yahoo.com wrote:
如果我想打电话给其他人怎么办?方法呢?修改你的例子
位,我希望调用Child的reset()方法调用
Mother和Father reset()方法,而不是通过名称引用它们,即ie ,Mother.reset(自我)。
What if I want to call other methods as well? Modifying your example a
bit, I''d like the reset() method call of Child to invoke both the
Mother and Father reset() methods, without referencing them by name,
i.e., Mother.reset(self).




[示例剪辑]


这个问题并不是不兼容的签名但是,在菱形的

继承图的顶部缺少一个

实现的reset()方法,它不会调用超类方法。那个方法

基本上可以是一个noop:

class Base(对象):

def reset(self):

#object为__init执行类似__()

传递


class母亲(基础):

def reset(self ):

打印重置母亲

super(母亲,自我).reset()


class Father(Base ):

def reset(self):

print" resetting Father"

super(父亲,自我).reset()


class Child(母亲,父亲):

def reset(self):

print" resetting Child"

super(孩子,自我).reset()

孩子()。重置()


如果有这样的方法,有时可能会很方便对于对象类来说,神奇地存在于
存在,但是现在你必须手动完成它(或者

也许有人提出了一个我不知道的元类)。


彼得


PS:见 http://www.python.org/2.2/descrintro.html 了解有关此主题的更多信息。



[example snipped]

The problem with that aren''t incompatible signatures, but the lack of an
implementation of the reset() method at the top of the diamond-shaped
inheritance graph that does _not_ call the superclass method. That method
could be basically a noop:
class Base(object):
def reset(self):
# object does something similar for __init__()
pass

class Mother(Base):
def reset(self):
print "resetting Mother"
super(Mother, self).reset()

class Father(Base):
def reset(self):
print "resetting Father"
super(Father, self).reset()

class Child(Mother, Father):
def reset(self):
print "resetting Child"
super(Child, self).reset()
Child().reset()

It might sometimes be convenient if such methods would magically spring into
existence for the object class, but for now you have to do it manually (or
perhaps someone has come up with a metaclass that I am not aware of).

Peter

PS: See http://www.python.org/2.2/descrintro.html for more on the subject.


这篇关于使用多重继承调用__init__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆