简体   繁体   English

如何使用继承的类方法并维护子类

[英]How to use inherited class method and maintain the subclass

I'm working through the Building Skills in Object Oriented Design in python and am on the wheel section for roulette.我正在学习 Python面向对象设计中的构建技能,并且正在轮盘赌的轮盘部分 We've created a "Bin" class as an extended class from frozenset which will represent each of the positions on the roulette wheel.我们已经创建了一个“Bin”类作为来自frozenset 的扩展类,它将代表轮盘赌上的每个位置。 We then create a tuple of 38 empty "Bins", and now have to create class methods to be able to add odds/outcomes to the Bins.然后我们创建一个包含 38 个空“垃圾箱”的元组,现在必须创建类方法才能将赔率/结果添加到垃圾箱。

My problem is that I've not been able to create a method to modify the Bin in position without the result not reverting to the frozenset class.我的问题是我无法创建一种方法来修改 Bin 的位置,而结果不会恢复到frozenset 类。

My desired output is to have:我想要的输出是:

class Bin(frozenset):
     def add(self, other):
          ....do union of Bin class....

one = Bin(1, 2, 3)
two = Bin(4, 5)
one.add(two)
print(one)
>>> Bin(1, 2, 3, 4, 5)

Stuff I've tried我试过的东西

Extending the frozenset class with no methods defined/overridden在没有定义/覆盖任何方法的情况下扩展frozenset类

class Bin(frozenset):
     pass

one = Bin([1,2,3])
two = Bin([4,5,6])
print(one|two)
print(type(one|two))

Which returns哪个返回

frozenset({1, 2, 3, 4, 5, 6})
<class 'frozenset'>   

I would have expected that by extending the class and using one of the extended methods that the output would remain as the "Bin" class.我原以为通过扩展类并使用扩展方法之一,输出将保留为“Bin”类。

I've also tried overriding the __ ror__ & union methods with the same result.我还尝试以相同的结果覆盖 __ ror__ 和 union 方法。 I've tried to create a method which to brute force return the desired output.我试图创建一个方法来蛮力返回所需的输出。 This however does not allow me to change the tuple of Bins as it doesn't operate in place但是,这不允许我更改 Bins 的元组,因为它没有就地运行

class Bin(frozenset):

def add(self, other):
    self = Bin(self|other)
    return self
one = Bin([1,2,3])
two = Bin([4,5,6])
one.add(two)
print(one)

Which returns哪个返回

Bin({1, 2, 3})

Any insight into where in falling down in my thinking would and/or recommendations of stuff to read for further insight would be great.任何关于我的思想中跌倒的地方的见解和/或建议阅读以进一步了解的东西都会很棒。

frozenset.__or__ (which is called by the default implementation of Bin.__or__ when 'triggered' by one | two ) has no idea that frozenset was subclassed by Bin , and that it should return a Bin instance. frozenset.__or__ (当由one | two '触发'时由Bin.__or__的默认实现调用)不知道frozenset是由Bin子类化的,并且它应该返回一个Bin实例。

You should implement Bin.__or__ and force it to return a Bin instance:你应该实现Bin.__or__并强制它返回一个Bin实例:

class Bin(frozenset):
    def __or__(self, other):
        # be wary of infinite recursion if using | incorrectly here,
        # better to use the underlying __or__
        return Bin(super().__or__(other))

one = Bin([1, 2, 3])
two = Bin([4, 5, 6])
print(one | two)
print(type(one | two))

Outputs输出

Bin({1, 2, 3, 4, 5, 6})
<class '__main__.Bin'>

You need to do something like this (to avoid infinite recursion):你需要做这样的事情(以避免无限递归):

class Bin(frozenset):
    def __or__(self, other):
        return Bin(frozenset(self) | other)

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

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