Python复合模式异常处理& 皮林特
我正在这种方式实现复合模式:
1)“抽象”组件是:
class Component(object):
"""Basic Component Abstraction"""
def __init__(self, *args, **kw):
raise NotImplementedError("must be subclassed")
def status(self):
"""Base Abstract method"""
raise NotImplementedError("must be implemented")
2)叶子:
class Leaf(Component):
"""Basic atomic component
"""
def __init__(self, *args, **kw):
self.dict = {}
def status(self):
"""Retrieves properties
"""
return self.dict
问题是 pylint 当然会生成此警告:
Leaf.__init__: __init__ method from base class 'Component' is not called
但在我的叶子中,我无法调用:
def __init__(self, *args, **kw):
Component.__init__(self, *args, **kw)
self.dict = {}
不引发异常。
我必须忽略 pylint 警告还是存在一些错误的编码?
I'm implementig a Composite pattern in this way:
1) the "abstract" component is:
class Component(object):
"""Basic Component Abstraction"""
def __init__(self, *args, **kw):
raise NotImplementedError("must be subclassed")
def status(self):
"""Base Abstract method"""
raise NotImplementedError("must be implemented")
2) a leaf:
class Leaf(Component):
"""Basic atomic component
"""
def __init__(self, *args, **kw):
self.dict = {}
def status(self):
"""Retrieves properties
"""
return self.dict
The Problem is that pylint generates, of course, this warning:
Leaf.__init__: __init__ method from base class 'Component' is not called
but into my Leaf i cannot call for:
def __init__(self, *args, **kw):
Component.__init__(self, *args, **kw)
self.dict = {}
without raise of the exception.
Must I ignore pylint warning or there is some bad coding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
抽象初始化器是一个坏主意。 您的代码可能会演变,以便您想要在根组件中进行一些初始化。 即使您不知道为什么需要实现初始化程序。 对于某些子类,空的初始值设定项是可以接受的选择。
如果您不需要 Component 类的任何实例,请在初始化程序中检查:
Abstract initializers are a bad idea. Your code might evolve so that you want to do some initialization in the root component. And even if you don't why require the implementation of the initializer. For some subclasses an empty initializer would be an acceptable choice.
If you don't want any instances of the Component class around, check for that in the initializer:
补充 Markus 想法的另一个建议:
如果您确实必须这样做,我建议您使用 __new __ 并检查给定的对象类型。 当它是“Component”时,您可以触发异常:
创建子类时,objType 将是!= Component,一切都会好起来的!
Another suggestion to complement the idea of Markus:
If you really must, I suggest that you use __new __ and check for the given object type. When it is "Component" you could fire your exception:
When a subclass is created, objType will be != Component and all will be fine!
将类 Component 重命名为 AbstractComponent 应该会有所帮助。 如果子类不调用该方法,则不要在基类中提供 __init__ 方法。
Renaming your class Component to AbstractComponent should help. And don't provide an
__init__
method in your base class if it's not supposed to be called by subclasses.您想要保证基类 Component 没有被实例化。 这是其他编程语言(如 C++)中常见的高贵姿态(您可以将构造函数设置为私有,以防止直接使用)。
但Python不支持它。 Python 不支持所有编程概念,而且更加“动态”。 因此,初始化是以“Pythonic”方式完成的,因此不支持您的概念。
与其他语言相比,Python 更多地基于信任——因此,例如,不支持静态变量,并且私有变量也仅以有限的方式支持。
您可以做什么(当您不信任模块的用户时)——您可以通过将基类命名为“_Component”来隐藏它——使其成为内部秘密。 但这当然会带来其他麻烦。
You want to guarantee, that the base class Component is not instanciated. This is a noble guesture common in other programming languages like C++ (where you can make the constructors private or so to prevent direct usage).
But it is not supported in Python. Python does not support all programming notions and also is more "dynamic". So initialization is done in a "Pythonic" way and your notion is not supported thus.
Python is much more based on trust than other languages -- so, for example static variables are not supported and private ones also only in a limited way.
What you could do (when you distrust the users of your module) -- you could hide the base class by naming it "_Component" -- make it an internal secret. But of course this can create other troubles.
这样的编码不错,但是根本不需要组件的
__init__
。 如果你想要它,你可以忽略 pylint,但更好的主意是简单地从组件中删除__init__
。拥抱动态主义!
Not bad coding as such, but the
__init__
of the component is simply not needed. If you want it, you can ignore pylint, but it's a better idea to simply remove the__init__
from Component.Embrace the dynamicism!