Python 还有什么其他的方式来称呼家长';除了super()之外的s_uuinit_uu方法?

Python 还有什么其他的方式来称呼家长';除了super()之外的s_uuinit_uu方法?,python,Python,我试图让子类调用并使用父类的_init__方法,除了在子类中使用super()方法之外,还有什么其他方法可以做到这一点?我被告知避免使用super()是可能的,因此我想知道 # -*- coding: utf-8 -*- class Room(object): def __init__(self, current_room): self.current_room = current_room print("You are now in Room #{}

我试图让子类调用并使用父类的_init__方法,除了在子类中使用super()方法之外,还有什么其他方法可以做到这一点?我被告知避免使用super()是可能的,因此我想知道

# -*- coding: utf-8 -*-

class Room(object):

    def __init__(self, current_room):
        self.current_room = current_room
        print("You are now in Room #{}".format(self.current_room))

class EmptyStartRoom(Room):

    def __init__(self, current_room=1):
        super().__init__(current_room)

class ChestRoomKey1(Room):

    def __init__(self, current_room=2):
        super().__init__(current_room)

a_room = EmptyStartRoom()
other_room = ChestRoomKey1()
从上面的代码中,我得到:

你现在在1号房间


您现在在2号房间

您可以直接调用基类,同时传递
self
参数:

# -*- coding: utf-8 -*-
class Room(object):    
    def __init__(self, current_room):
        self.current_room = current_room
        print("You are now in Room #{}".format(self.current_room))

class EmptyStartRoom(Room):    
    def __init__(self, current_room=1):
        Room.__init__(self, current_room)

class ChestRoomKey1(Room):    
    def __init__(self, current_room=2):
        Room.__init__(self, current_room)

a_room = EmptyStartRoom()
other_room = ChestRoomKey1()

您也应该检查POST,告诉您为什么在开始多继承时应该考虑使用<代码>(或)>代码,但现在,两种方式都很好。

< P>不要尝试寻找替代方案。

它们可能是可能的,但最终您将硬编码超类(参见@abccd-answer)或使用“您自己的解决方案”。但是,从长远来看,避免使用
super()
将成为维护的噩梦(现在更难实现)


就你而言,你做的一切都是对的!这个例子有点奇怪,因为
\uuu init\uuu
方法之间的唯一区别是参数的默认值,但我想这只是为了说明问题,对吗?

为什么要避免使用
super()
?@Blender我被告知使用super()有好处也有坏处所以作为一个初学者,如果我不确定我在做什么,我应该尽量避免使用它。也许不是这样?