python类的组成,Python类结构。。。prep()方法?

我们有一个用于警报系统的元类、类和子类:class AlertMeta(type):

"""

Metaclass for all alerts

Reads attrs and organizes AlertMessageType data

"""

def __new__(cls, base, name, attrs):

new_class = super(AlertMeta, cls).__new__(cls, base, name, attrs)

# do stuff to new_class

return new_class

class BaseAlert(object):

"""

BaseAlert objects should be instantiated

in order to create new AlertItems.

Alert objects have classmethods for dequeue (to batch AlertItems)

and register (for associated a user to an AlertType and AlertMessageType)

If the __init__ function recieves 'dequeue=True' as a kwarg, then all other

arguments will be ignored and the Alert will check for messages to send

"""

__metaclass__ = AlertMeta

def __init__(self, **kwargs):

dequeue = kwargs.pop('dequeue',None)

if kwargs:

raise ValueError('Unexpected keyword arguments: %s' % kwargs)

if dequeue:

self.dequeue()

else:

# Do Normal init stuff

def dequeue(self):

"""

Pop batched AlertItems

"""

# Dequeue from a custom queue

class CustomAlert(BaseAlert):

def __init__(self,**kwargs):

# prepare custom init data

super(BaseAlert, self).__init__(**kwargs)

我们希望能够生成BaseAlert(CustomAlert)的子类,这些类允许我们运行出列并能够运行它们自己的__init__代码。我们认为有三种方法可以做到这一点:添加一个prep()方法,该方法在BaseAlert中返回True,并由__init__调用。子类可以定义自己的prep()方法。在

使dequeue()成为一个类方法—但是,dequeue()所做的很多工作都需要非类方法—因此我们也必须使这些类方法成为类方法。在

创建一个新类来处理队列。这个类会扩展BaseAlert吗?在

有没有处理这种情况的标准方法?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值