Passing an instance of a class to a subclass

I use boto to control EC2 . It provides an instance class. I would like to subclass it to meet my specific needs. Since boto provides a query interface to get your instances, I need to convert something between classes. This solution seems to work, but changing the attribute of the class seems dodgy. Is there a better way?

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
+3
source share
1 answer

I would not subclass or drop. I don't think casting is a good policy.

Instead, consider a wrapper or facade.

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

MyThing , , boto.ec2.instance.Instance. .

+7

Source: https://habr.com/ru/post/1709525/


All Articles