简体   繁体   English

在Python 2的子类中重写__init__方法的一部分

[英]Overriding part of the __init__ method in a subclass in Python 2

I'm wanting to create a subclass of a subclass (Barrier is a type of wall, which is a type of obstacle), and I'm wanting the barrier to have the same init method as wall with the exception that the self.type = 'barrier', but I'm not sure how to do this (I'm very new to programming so I'm sorry if this is very simple but I haven't been able to find an answer I understand). 我想创建一个子类的子类(屏障是墙壁的一种,这是障碍的一种),并且我希望屏障具有与壁相同的init方法,但self.type除外='barrier',但我不确定如何执行此操作(我对编程很陌生,如果这很简单,但我找不到我理解的答案,我感到抱歉)。 So far I have: 到目前为止,我有:

class Obstacle:
    def __init__(self, type):
        self.type = 'obstacle'

    def __str__(self):
        return "obstacle"

class Wall(Obstacle):

    def __init__(self, origin, end):

        self.type = 'wall'
        self.origin = origin
        self.end = end

        # etc.... (i.e. there are more things included in here which the 
        # that the barrier also needs to have (like coordinate vectors etc.)

class Barrier(Wall):

    def __str__(self):
        return "Barrier obstacle"

How do I change it so that class Barrier has identical contents of the init method as Walls do, except their "self.type = 'barrier'"? 如何更改它,使Barrier类的初始化方法内容与Walls相同,除了它们的“ self.type ='barrier'”?

Just override that one attribute after invoking the Wall version: 在调用Wall版本后,只需覆盖该属性即可:

class Barrier(Wall):
    def __init__(self, origin, end):
        super().__init__(origin, end)
        self.type = 'barrier'

    def __str__(self):
        return "Barrier obstacle"

You may want to consider using class attributes instead however; 但是,您可能要考虑使用类属性。 none of your instance attributes are dynamic and specific to each instance of the class. 您的实例属性没有一个是动态的,并且特定于该类的每个实例。 The type attribute of each of these classes surely won't change from one instance to another: 每个这些类的type属性肯定不会从一个实例更改为另一个:

class Obstacle:
    type = 'obstacle'

    def __str__(self):
        return self.type

class Wall(Obstacle):
    type = 'wall'

    def __init__(self, origin, end):
        super().__init__()
        self.origin = origin
        self.end = end
        # etc.... (i.e. there are more things included in here which the 
        # that the barrier also needs to have (like coordinate vectors etc.)

class Barrier(Wall):
    type = 'barrier'

Since the "type" seems to depend on the class, I would simply not put the type attribute in the object , but at the class level : 由于“类型”似乎取决于类,因此我不会简单地type属性放在对象中 ,而是放在类级别

class Obstacle:

    type = 'obstacle'

    def __init__(self):
        # do something
        pass

    def __str__(self):
        return "obstacle"

class Wall(Obstacle):

    type = 'wall'

    def __init__(self, origin, end):
        super().__init__()
        self.origin = origin
        self.end = end

class Barrier(Wall):

    type = 'barrier'

    def __str__(self):
        return "Barrier obstacle"

Furthermore you better call the super().__init__ method if you override it. 此外,如果覆盖它,则最好调用super().__init__方法。 Since otherwise initializations higher in the class hierarchy will not take place (this is sometimes desired behavior, but usually it is not). 因为否则将不会进行类层次结构中更高级别的初始化(这有时是所需的行为,但通常不是)。

The advantage of this is that it is - in my opinion at least - more elegant. 这样做的好处是-至少在我看来-更优雅。 Since here it is clear that type is defined per class. 因为在这里很明显, type每类中定义。 But furthermore it will reduce the amount of memory that is used. 但是此外,它将减少使用的内存量。 Since we store one attribute per class, not one per object. 由于我们为每个类存储一个属性, 而不是为每个对象存储一个属性。

However in case you want to alter the attribute of a single object, that is still possible. 但是,如果您要更改单个对象的属性,则仍然可以这样做。 For instance: 例如:

>>> obs1 = Obstacle()
>>> weird_obs = Obstacle()
>>> weird_obs.type = 'weird obstacle'
>>> obs2 = Obstacle()
>>> obs1.type
'obstacle'
>>> weird_obs.type
'weird obstacle'
>>> obs2.type
'obstacle'

So we still have the flexibility to add a specific type to a specific object. 因此,我们仍然可以灵活地向特定对象添加特定类型。 But by default, if we query an obstacles type , it will perform a fallback and return the type that is defined on the class level. 但默认情况下,如果我们查询一个障碍type ,它会执行回退,返回type是在类级别定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM