{general} - Looping parameter settings for methods


#1

When setting parameters for methods within a class, is it possible to use a loop? For example, instead of:

class Triangle(object):
def __init__(self, angle1, angle2, angle3):
    self.angle1 = angle1
    self.angle2 = angle2
    self.angle3 = angle3

I would like to do something along the lines of:

class Triangle(object):
def __init__(self, angle1, angle2, angle3):
    for x in range(0,3):
        "self.angle"+str(x) = "angle"+str(x)

But this returns an error

SyntaxError: can't assign to operator

Is something like this possible or do you have to assign parameters by hand?

Thanks in advance.


#2

You can send in a list. Typically if you need to loop through something it's because they are all part of the same thing, and then it makes sense to make a list out of them. (or some other iterable)


#3

Thank you, I’ve tried a few variations on list, feel like I’m close but its still not picking up the parameters, I think it might be a simple syntax thing.

Back in action script (a thousand years ago) I would write something like:

self['side'+str(x)]

this is what i’ve tried:

def __init__(self, side1, side2, side3):
    sides = []
    for x in range(1, 4):
        sides.append('self.side'+str(x))
        sides[x] = 'side'+str(x)