简体   繁体   English

处理班级中预设选项的最佳方法

[英]Best way to handle preset options in classes

I want to have a Class which can be initialized with options a , b and c . 我想有一个可以使用选项abc初始化的类。

c is a special case, where I can modify the initialization with a variable extend . c是一种特殊情况,其中我可以使用变量extend修改初始化。

I'm currently looking for the best way to do this. 我目前正在寻找执行此操作的最佳方法。 Also I would like my IDE (in this case PyCharm) to make suggestions to me which parameters I can use for the preset. 另外,我希望我的IDE(在本例中为PyCharm)向我建议可以用于预设的参数。

I came up with two ideas to do it. 我想出了两个主意。

Option 1: 选项1:

class MyClass:
    def __init__(self,preset,extend=None):
        if preset == "a":
            self.x = 1
        if preset == "b":
            self.x = 2
        if preset == "c":
            self.x = 3
            if extend != None:
                self.x = self.x + extend
    def __str__(self):
        return f"The value of x is {self.x}"

Y=MyClass(preset="c",extend= 3)
print(Y)
#out: The value of x is 6

Option 2: 选项2:

class MyClass2:
    def __init__(self):
        self.x=None
    def preset_a(self):
        self.x=1
    def preset_b(self):
        self.x=2
    def preset_c_with_extend(self,extend):
        self.x =3+extend

    def __str__(self):
        return f"The value of x is {self.x}"

Y2=MyClass2()
Y2.preset_b()
print(Y2)
#out: The value of x is 2

Option 1 looks more elegant to me, but in my workflow I don't want to go to the implementation for initializing a certain preset for looking up the options. 选项1对我来说看起来更优雅,但是在我的工作流程中,我不想去执行初始化某个用于查找选项的预设的实现。

But this would be necessary, because I can not remember for bigger projects if I named the preset a or if it was not A . 但这是必须的,因为如果我将预设命名为a或不是A ,我将无法记住更大的项目。 Option 1 also leaves it unclear if I can add an option extend . 选项1还不清楚我是否可以添加extend选项。 Here it might happen, that I use preset a with extend=3 and I am wondering why the extend is not applied. 在这种情况下,可能会发生,我将预设aextend=3 ,我想知道为什么不应用扩展。

So the actual question: Is there an elegant way to see the preset options without looking at the class implementation? 因此,实际的问题是:是否有一种优雅的方式可以在不查看类实现的情况下查看预设选项? (Maybe some kind of Type Hint?) (也许是某种类型的提示?)

Option 2 has this opportunity, and with auto-completion in my IDE I see what presets I can apply. 选项2拥有这个机会,并且在我的IDE中具有自动完成功能之后,我看到了可以应用哪些预设。 But it doesn't look very elegant. 但这看起来并不优雅。

I am curious about your ideas! 我对你的想法很好奇!

How about: 怎么样:

class MyClass2:
    def __init__(self, x):
        self.x = x
    @staticmethod
    def preset_a():
        return MyClass2(1)
    @staticmethod
    def preset_b():
        return MyClass2(2)
    @staticmethod
    def preset_c_with_extend(extend):
        return MyClass2(3+extend)

    def __str__(self):
        return f"The value of x is {self.x}"

Y2=MyClass2.preset_b()
print(Y2)

It ensures that x is set at object creation time and should allow IDE auto-completion. 它确保x在对象创建时设置,并且应该允许IDE自动完成。

Another alternative is to use a presets dict . 另一种选择是使用预设dict However, I have no idea how PyCharm will treat this solution in regards to suggestions. 但是,我不知道PyCharm在建议方面将如何对待该解决方案。

class MyClass:
    PRESETS = {"a": 1, "b": 2, "c": 3}

    def __init__(self, preset, extend=None):
        self.x = self.PRESETS.get(preset)

        if preset == "c" and extend is not None:
            self.x += extend

    def __str__(self):
        return f"The value of x is {self.x}"

Note that dict 's .get() method is used which means x will be None if you try to use a non-existing preset. 请注意,使用了dict.get()方法,这意味着如果您尝试使用不存在的预设,则x将为None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM