python 基类初始化_python:抽象基类’__init __():初始化或验证?

当然,一个人喜欢方法1接近2(因为方法2将基数降级到标签接口而不是实现抽象功能).但是,方法1本身不能满足您防止子类型开发人员忘记实施super()调用的目标,从而确保初始化.

您可能想查看“工厂”模式,以减轻子类型实施者遗忘初始化的可能性.考虑:

class AbstractClass(object):

'''Abstract base class template, implementing factory pattern through

use of the __new__() initializer. Factory method supports trivial,

argumented, & keyword argument constructors of arbitrary length.'''

__slots__ = ["baseProperty"]

'''Slots define [template] abstract class attributes. No instance

__dict__ will be present unless subclasses create it through

implicit attribute definition in __init__() '''

def __new__(cls, *args, **kwargs):

'''Factory method for base/subtype creation. Simply creates an

(new-style class) object instance and sets a base property. '''

instance = object.__new__(cls)

instance.baseProperty = "Thingee"

return instance

这种基础类可以比方法1更简单地扩展,只使用三(3)行代码san-commment,如下所示:

class Sub(AbstractClass):

'''Subtype template implements AbstractClass base type and adds

its own 'foo' attribute. Note (though poor style, that __slots__

and __dict__ style attributes may be mixed.'''

def __init__(self):

'''Subtype initializer. Sets 'foo' attribute. '''

self.foo = "bar"

请注意,虽然我们没有调用超类的构造函数,但是baseProperty将被初始化:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)

[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> from TestFactory import *

>>> s = Sub()

>>> s.foo

'bar'

>>> s.baseProperty

'Thingee'

>>>

正如其注释所示,基类AbstractClass不需要使用插槽,它可以通过将其设置在new()初始化器中来轻松地“隐式”定义属性.例如:

instance.otherBaseProperty = "Thingee2"

会工作正常还要注意,基类“初始化器”在其子类型中支持简单(无参数)初始化器,以及可变长度的参数初始化器和关键字参数初始化器.我建议始终使用此表单,因为它不会在最简单(简单的构造函数)情况下强加语法,但允许更复杂的功能,而不强加维护.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值