直接从父对象实例化子对象

发布于 2025-01-29 06:31:34 字数 452 浏览 5 评论 0原文

我有两个从另一个对象继承,它们之间的唯一区别是一些属性字段:

class Parent:
    def __init__(self,a, b):
        self.a = a
        self.b = b

    def methodA(self):
        # do something
        pass

class Child(Parent):
    def __init__(self,c,**kwargs):
        self.c = c
        super().__init__(**kwargs)

我有一个父对象的实例,我想在python中找到一种快速的方法来创建一个只有一个只有具有的子对象的实例使用已经存在的父对象的另一个字段。

是否有Python或模块可以让您轻松地做到这一点。在我的真实代码中,父类有数百个字段,仅重新分配其值有点低效。

I have two objects one inherits from the other and the only difference between them is a few attribute fields:

class Parent:
    def __init__(self,a, b):
        self.a = a
        self.b = b

    def methodA(self):
        # do something
        pass

class Child(Parent):
    def __init__(self,c,**kwargs):
        self.c = c
        super().__init__(**kwargs)

I have an instance of the parent object and I want to find a fast way in python to create an instance of the child object which only has one additional field by using the already existing parent object.

Is there a python way or module that lets you do that easily. IN my real code the parent class has hundreds of fields and it is a bit inefficient to just reassign its value.

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

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

发布评论

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

评论(2

两相知 2025-02-05 06:31:34

规范的解决方案是将类方法添加到Child作为构造函数的。它采用parent实例,然后返回带有适当属性的child实例。

例如:

class Parent:
    def __init__(self,a, b):
        self.a = a
        self.b = b


class Child(Parent):
    def __init__(self,c,**kwargs):
        self.c = c
        super().__init__(**kwargs)

    @classmethod
    def from_parent(cls, parent, c):
        return cls(a=parent.a, b=parent.b, c=c)


p = Parent(a=1, b=2)
c = Child.from_parent(parent=p, c=3)
print(c.a, c.b, c.c)  # output: 1 2 3

我认为您的parent具有数百个属性的类与答案无关。是的,必须在from_parent方法中明确地写出parent> parent实例的每个属性是很乏味的,但这只是一个限制,无论如何还是要有一个具有许多属性的类。可能的是,更好的设计选择是将parent>属性>属性的组封装到适当的类中,以便在初始化后只需要将这些实例交付到child类。

The canonical solution is to add a class method to Child that works as a constructor. It takes a Parent instance and returns the Child instance with the proper attributes.

For example:

class Parent:
    def __init__(self,a, b):
        self.a = a
        self.b = b


class Child(Parent):
    def __init__(self,c,**kwargs):
        self.c = c
        super().__init__(**kwargs)

    @classmethod
    def from_parent(cls, parent, c):
        return cls(a=parent.a, b=parent.b, c=c)


p = Parent(a=1, b=2)
c = Child.from_parent(parent=p, c=3)
print(c.a, c.b, c.c)  # output: 1 2 3

I would argue that your Parent class having hundreds of attributes is irrelevant to the answer. Yes, it's tedious having to explicitly write every attribute of the Parent instance in the from_parent method, but that's simply a limitation of having a class with that many attributes anyway. Possibly, a better design choice would be to encapsulate groups of Parent attributes into proper classes, so that only those instances need to be delivered to the Child class upon initialization.

吃素的狼 2025-02-05 06:31:34

好的,制作一种使用父属性并创建子对象的方法的其他建议是可以的,但我认为添加了不必要的代码。我做了这个解决方案,最终使用了。它不直接接受父对象作为参数,但我认为这更简洁:

class Parent:
    def __init__(self, a, b):
        self.a = a
        self.b = b
        
class Child(Parent):
    def __init__(self,c, **kwargs):
        self.c = c
        super().__init__(**kwargs)

# So if I start with this parent object
parent_args = {"a":23,"b":"iuhsdg"}
parent =Parent(**parent_args)

# I then make child with all the parent attributes plus some more
child_args = {"c":567}
child_args.update(vars(parent))
child = Child(**child_args)

Ok the other suggestions for making a method that takes in parent attributes and creates a child object is ok but adds unnecessary code I think. I made this solution, which I ended up using. It doesnt accept the parent object directly in as an argument but it is more concise I think:

class Parent:
    def __init__(self, a, b):
        self.a = a
        self.b = b
        
class Child(Parent):
    def __init__(self,c, **kwargs):
        self.c = c
        super().__init__(**kwargs)

# So if I start with this parent object
parent_args = {"a":23,"b":"iuhsdg"}
parent =Parent(**parent_args)

# I then make child with all the parent attributes plus some more
child_args = {"c":567}
child_args.update(vars(parent))
child = Child(**child_args)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文