Reputation: 484
I am implementing a class with a huge amount of variables. In that class, I want to do a get function and a set function, these functions will have 4 (2 in the example) arguments. For example:
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
As you can see, my problem is that switch-case \ if-elif-else part, that I couldn't find a way to export to one function. What is the beautiful python way to do it?
Upvotes: 1
Views: 96
Reputation: 73470
E.g. use an array variable and calculate an index from your arguments:
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
Upvotes: 1
Reputation: 2332
Why not something like the following:
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
Then you just have something like:
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"
Not sure if you're constrained by other conditions, but you can certainly simplify this process by making all your var
parameters a value in a list or array and indexing it based on arg1
and arg2
values. You can create an index numpy array like:
self.index = numpy.array([[0,1],[2,3]])
then you variable array would be
self.var = np.array([0,0,0,0])
Now your method doesn't need all those 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]]
Upvotes: 0
Reputation: 27293
As ubadub said, it seems like you're not actually telling us what you want to achieve.
If my guess that you're trying to save a field of bits is correct, you can make the whole thing very simple:
def get(self, arg1, arg2):
return getattr(self, "var" + str(1+2*(1-arg2)+(1-arg1)))
But in that case, there are much better ways.
Upvotes: 1
Reputation: 2852
Not sure I fully grasp it; and if you're asking how to merge the get function with the set function, I don't think that makes sense (as formulated) - either you're setting values on an instance, or getting values that are already 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]
But I might rather try something like:
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]
Upvotes: 0