如何实现“参数对象”在 Python 中重构?

发布于 2024-08-16 07:03:48 字数 729 浏览 5 评论 0原文

现在,我使用参数对象的类来继承,如下所示:

class A():
    def __init__(self, p1, p2):
        self.p1, self.p2 = p1, p2

class B(A):
    def __init__(self, b):
        self.p1, self.p2 = b.p1, b.p2

这消除了使用代码而不是类代码本身的荒谬性。所以,我想做 C++ 的事情并将参数对象传递到初始化列表,如下所示:

class A { 
    int p1, p2; 
}
class B : public A { 
    B(const A& a) : A(a) {} 
}

我可以在 Python 中执行此操作吗?具体来说,我可以通过在子类中调用它的 __init__ 来设置父类的属性吗? - 通过阅读“Dive into Python”,我想我可以做到这一点,因为在调用 __init__ 时该对象已经构造完毕。

或者,也许有一些不同的方法在 Python 中实现 参数对象重构 (并且我只是想强制使用 C++ 技术)?

Right now I use the parameter object's class to be inherited like so:

class A():
    def __init__(self, p1, p2):
        self.p1, self.p2 = p1, p2

class B(A):
    def __init__(self, b):
        self.p1, self.p2 = b.p1, b.p2

This trims up the absurdity of using the code but not the class code itself. So, I'd like to do the C++ thing and pass the parameter object to the initialization list like so:

class A { 
    int p1, p2; 
}
class B : public A { 
    B(const A& a) : A(a) {} 
}

Can I do this in Python? Specifically, can I set the attributes of a parent class by somehow calling it's __init__ within the child's? - from reading "Dive into Python" I'd guess I can do this since the object is already constructed by the time __init__ is called.

Or, perhaps is there some different method of implementing the parameter object refactor in Python (and I'm just trying to force a C++ technique)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

榆西 2024-08-23 07:03:48

一般来说,您可能不需要参数对象重构技术。 Python 标准库中有多个集合,它们已经具有参数对象重构所提供的核心优势。

此外,参数对象设计模式等重构的成本/收益分析与 C++ 显着不同。例如,如果没有静态编译,对象创建和成员查找的成本要高得多。参数对象模式的基本情况是为相关字段提供命名空间,这在 Python 中可以通过字典简单地完成。

如果您需要以与分组数据相关的方法的形式进行某种模型操作,则该模式在 Python 中具有更大的价值,但即使如此,像“属性”这样简单且廉价的机制也可以解决成员属性的操作需求。

通过对问题空间进行更详细的描述,我们可能可以帮助评估这种重构是否有好处,但很可能在使用 Python 时最好不要使用该设计模式。

Generally speaking, you likely don't need the parameter object refactor technique. Python has several collections in the standard library that already have the core benefits that a parameter object refactor would provide.

Additionally, the cost/benefit analysis for a refactor like the parameter object design pattern is notably different than C++. For instance, without static compilation, object creation and member lookups are much more expensive. The base case of the parameter object pattern is to provide a namespace for related fields, something that can be accomplished simply enough in Python with a dictionary.

The pattern has more value in Python if you need some kind of model manipulation in the form of methods that are related to the grouped data, but even then mechanisms as simple and inexpensive as "properties" can solve manipulation needs on member attributes.

With a slightly more detailed description of the problem space, we can probably help evaluate whether this sort of refactor is a benefit, but most likely you'd be better not using that design pattern when working in Python.

漫漫岁月 2024-08-23 07:03:48

将 C++ 代码翻译成 python 会是这样的:

class A():
    def __init__(self,p1,p2):
        self.p1, self.p2 = p1, p2

class B(A):
    def __init__(self,b):
        A.__init__(self, b.p1, b.p2)

The translation of the C++ code into python would be something like:

class A():
    def __init__(self,p1,p2):
        self.p1, self.p2 = p1, p2

class B(A):
    def __init__(self,b):
        A.__init__(self, b.p1, b.p2)
太阳公公是暖光 2024-08-23 07:03:48
class B(A):
    def __init__(self, *args, **kwargs):
        super(B, self).__init__(*args, **kwargs)

阅读有关 super() 的详细信息。

class B(A):
    def __init__(self, *args, **kwargs):
        super(B, self).__init__(*args, **kwargs)

Read more about super() for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文