1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python给函数添加属性_如何在python中自动向类添加属性?

python给函数添加属性_如何在python中自动向类添加属性?

时间:2022-04-29 15:27:53

相关推荐

python给函数添加属性_如何在python中自动向类添加属性?

我有一个具有很多属性的类,这些属性在instanciating(init)时提供。

看起来像这样,但还有大约30个attr:class SomeObject:

def __init__(self,

first,

second):

self.first = first

self.second = second

工作正常,但很长很重复,很难更新。

在我使用更小的课程之前:class SomeObject:

def __init__(self, **attr):

self.__dict__.update(attr)

工作得很好,更容易,但很难跟踪。当使用IDE(我使用PyCharm)时,我在编写对象时没有提示,自动完成提示通常不起作用。

我会寻找两个类的混合,如:class SomeObject:

def __init__(self,

first,

second):

self.__dict__.update(???) # I would like to do it in a single line, so I dont have to write a new attr two (three...) times.

有谁知道这是否可行?非常感谢

Python 3.4 + PyCharm .1 CommunityEdition

...编辑/补充......

问题似乎主要是“保留”IDE中的代码检查,以便对象上仍然可以使用自动完成等。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。