gpt4 book ai didi

python - 继承自python的构造函数

转载 作者:太空宇宙 更新时间:2023-11-04 10:55:30 25 4
gpt4 key购买 nike

我做了一个看起来像这样的类。

class NewInt:
def __init__(self, x,y):
self.holders = {"x":x,"y":y}

现在,我有一本字典。所以一切都完全一样

除了它不是 NewInt 而是一个 dict.Somethng 之类的

 A  {"x": 3,"y":4}

所以它完全是 NewInt 类型,除了它是一个字典。

有什么方法可以做类似的事情

this_int = NewInt(A)

我忘了,但是完成赋值的构造函数有一个特殊的名称??谢谢

最佳答案

您可以使用 keyword arguments :

A = {"x": 3,"y":4}
this_int = NewInt(**A)

顺便说一句,看起来你正在重新实现 complex numbers .这些已经内置到 Python 中,请尝试 complex(3,4)3+4j

关于python - 继承自python的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10049755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com