PyCharm-为所有构造函数参数生成字段

问题描述 投票:3回答:1

我用构造函数定义了一个类:

class Example:
    def __init__(self, a, b, c):
        ...

现在,我想为每个构造函数参数声明一个字段,如下所示:

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

我想自动执行此操作。通过PyCharm的检查,我可以为参数生成字段,但是一次只能生成一个字段(作为Parameter x is not used的修复程序),这非常烦人。如何针对所有参数同时执行此操作?

我发现最接近的是PyCharm template for python class __init__ function,该解决方案并不灵活,因为它仅支持固定数量的参数。而且,我觉得这是必须在成熟的IDE中实现的功能,Idea允许这样做。

该帖子还引用了What is the best way to do automatic attribute assignment in Python, and is it a good idea?,它讨论了autoassign,似乎有很多缺点。

我的PyCharm版本是2019.1.3 CE。

python pycharm code-generation
1个回答
0
投票

我最近处理这个问题的方法是在函数中编写所有参数:xyz类def init

© www.soinside.com 2019 - 2024. All rights reserved.