我在使用 宝途 管理一些 EC2 实例。它提供了一个实例类。我想要的子类,以满足我的特别需要。因为宝途提供了一个接口查询得到你的情况下,我需要的东西之间的转换课程。这种解决方案似乎是工作,但改变的类属性似乎不可靠的。是否有一个更好的办法?

from boto.ec2.instance import Instance as _Instance

class Instance(_Instance):
    @classmethod
    def from_instance(cls, instance):
        instance.__class__ = cls
        # set other attributes that this subclass cares about
        return instance
有帮助吗?

解决方案

我不会子类和铸造。我不认为铸造永远是一项好政策。

相反,考虑包装器或外墙。

class MyThing( object ):
    def __init__( self, theInstance ):
        self.ec2_instance = theInstance 

现在,你也可以继承MyThing就像你想,你不应该需要在所有铸造的boto.ec2.instance.Instance。它仍然是在对象的更或多或少不透明元件。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top