首页 > 解决方案 > 实例化后从另一个类中创建一个动态类

问题描述

我需要帮助创建一个动态类,我已经阅读了许多教程和其他发布在这里的问题,但我就是不明白......这是我到目前为止所拥有的。(为了这个问题,我删除了很多不必要的行)

我正在寻找的是创建一个SpecificAsset 类,这样我就可以在购买和交易特定货币时为它们创建税收批次。此外,我想跟踪创建或销毁(出售/交易)的特定货币的数量,但我不确定该采取哪种方法。我一直更专注于尝试创建动态类。

---我目前拥有的,不起作用... -------

class AssetMeta(type):
    asset_classes = {}
    
    def __new__(cls, clsname, superclasses, attributedict):
        if clsname in cls.asset_classes:
            raise ValueError("Redefinition of Asset subclass '{}'".format(clsname))
        tp = type.__new__(cls, clsname, superclasses, attributedict)
        cls.asset_classes[clsname] = tp
        return tp

# --\/--\/-- what the class should look like --\/--\/--  

class SpecificAsset(metaclass=AssetMeta):
    def __init__(self, owner, priAssetType, priAsset, qty, altAsset):
        self.id                  = id(self)
        self.owner               = owner           # an instance of a class defined in another file
        self.creationDate        = datetime.now()
        
        self.priType             = priAssetType    # a string, either 'base' or 'quote'
        self.primary             = priAsset        # the Currency object this asset represents
        self.holdingQty          = Decimal(qty)    # amount of the primary asset currently held
        self.initialQty          = Decimal(qty)    # because small portions of this asset can be sold
        self.primary.holdingQty += Decimal(qty)    # add qty held to the total qty of the represented asset
        
        self.altType             = 'quote' if self.priType == 'base' else 'base'
        self.alternate           = altAsset        # the Currency object this asset is permitted to trade with
        self.altValue            = Decimal(0.00)   # calculated when function (to be defined) is called
        
        self.USDpointer          = self.primary.pointer
        self.pending             = False

class Currency:
    count = 0
    def __init__(self, name):
        Currency.count += 1
        self.id         = id(self)
        self.name       = name
        self.bases      = {}
        self.quotes     = {}
        self.holdingQty = Decimal(0.00)
        
    def point_to(self, CUR):
        self.pointer    = CUR
   
# function call within the Currency class, which is supposed to create the dynamic SpecificAsset class...
# ... also not working...

    def create_asset_class(name, BaseClass=SpecificAsset):
        def __init__(self, **kwargs):
            for key, value in kwargs.items():
                if key not in argnames:
                    raise TypeError("Argument %s not valid for %s" 
                        % (key, self.__class__.__name__))
                setattr(self, key, value)
            BaseClass.__init__(self, name)
        newclass = type(self.name, 
                        (???,),                   # not sure what my parent/base class should be, or if needed
                        {
                           "__init__": __init__
                           })
        return newclass

标签: pythonclassdynamic

解决方案


推荐阅读