合并get和set函数python

时间:2016-03-22 19:45:46

标签: python

我正在实现一个包含大量变量的类。在那个类中,我想做一个get函数和一个set函数,这些函数将有4个(示例中为2个)参数。 例如:

class MyClass(object):
    def __init__(self):
        self.var1 = 0
        self.var2 = 0
        self.var3 = 0
        self.var4 = 0

    def get(self, arg1, arg2):
        if arg1 and arg2:
            return self.var1
        elif not arg1 and arg2:
            return self.var2
        elif arg1 and not arg2:
            return self.var3
        else:
            return self.var4

    def set(self, arg1, arg2, value):
        if arg1 and arg2:
            self.var1 = value
        elif not arg1 and arg2:
            self.var2 = value
        elif arg1 and not arg2:
            self.var3 = value
        else:
            self.var4 = value

正如您所看到的,我的问题是switch-case \ if-elif-else部分,我无法找到导出到一个函数的方法。什么是美丽的蟒蛇方式呢?

4 个答案:

答案 0 :(得分:1)

正如ubadub所说,它似乎是you're not actually telling us what you want to achieve

如果我猜你正试图保存一个字段是正确的,你可以让整个事情变得非常简单:

def get(self, arg1, arg2):
    return getattr(self, "var" + str(1+2*(1-arg2)+(1-arg1)))

但在这种情况下,有much better ways

答案 1 :(得分:1)

E.g。使用数组变量并根据参数计算索引:

class MyClass(object):
    def __init__(self):
        self.var = 4 * [0]

    def index(self, arg1, arg2):
        return 2*int(bool(arg1)) + int(bool(arg2))  # possible results: 0, 1, 2, 3

    def get(self, arg1, arg2):
        return self.var[self.index(arg1, arg2)]

    def set(self, arg1, arg2, value):
        self.var[self.index(arg1, arg2)] = value

答案 2 :(得分:0)

为什么不能达到如下内容:

class MyClass(object):
    def __init__(self):
        self.var1 = 0
        self.var2 = 0
        self.var3 = 0
        self.var4 = 0

    def view(self, arg1, arg2, value = None):
        if arg1 and arg2:
            if value is not None: self.var1 = value
            return self.var1
        elif not arg1 and arg2:
            if value is not None: self.var2 = value
            return self.var2
        elif arg1 and not arg2:
            if value is not None: self.var3 = value
            return self.var3
        else:
            if value is not None: self.var4 = value
            return self.var4

然后你就会有:

myclass = MyClass()
myclass.view(0,1,1)   # Changes the value, returns to nothing
x = myclass.view(0,1) # Doesn't change value, returns to "x"

不确定您是否受到其他条件的限制,但您可以通过将所有var参数设置为列表或数组中的值并根据arg1对其进行索引来简化此过程和arg2值。您可以创建索引numpy数组,如:

self.index = numpy.array([[0,1],[2,3]])

那么你的变量数组将是

self.var = np.array([0,0,0,0])

现在你的方法并不需要所有那些if / elifs

def view(self, arg1, arg2, value = None)
    if value is not None: self.var[self.index[arg1,arg2]] = value
    return self.var[self.index[arg1,arg2]]

答案 3 :(得分:0)

不确定我是否完全掌握了它;如果你问如何将get函数与set函数合并,我认为这没有意义(如表中所述) - 要么你在实例上设置值,要么获取已设置的值。

class Something(object):

    def __init__(self, var1, var2, var3, var4):
        self.var1 = var1
        self.var2 = var2
        self.var3 = var3
        self.var4 = var4

    def getValue(self, chooseOdd, chooseFirst):
        if chooseOdd:
            choices = [self.var1, self.var3]
        else:
            choices = [self.var2, self.var4]
        index = 0 if chooseFirst else 1
        return choices[index]

但我宁愿尝试类似的事情:

class Something(object):
    def __init__(self, var1, var2, var3, var4):
        self.var1 = var1
        self.var2 = var2
        self.var3 = var3
        self.var4 = var4
        self.values = [var1, var2, var3, var4]

   def get(self, arg1, arg2):
       """ Either require, or manually convert, arg1, arg2 into 0, 1"""
       index = arg2 << 1 | arg1
       return self.values[index]