我们有一间美分、一间和一间儿童级的警戒系统:
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)
我们希望能够使“基地”(CustomAlert)的儿童班子能够管理,并能够操作自己的“_init__
。 我们认为,这样做有三种途径:
- Add a prep() method that returns True in the BaseAlert and is called by
__init__
. Child classes could define their own prep() methods. - Make dequeue() a class method - however, alot of what dequeue() does requires non-class methods - so we d have to make those class methods as well.
- Create a new class for dealing with the queue. Would this class extend BaseAlert?
是否有处理这种情况的标准方法?