5

Whats the best way to reset class attributes in Python.

I have a class whichs has about 20 class attributes, in my init I have

class MyClass:
    def __init__(self)
       self.time=0
       self.pos=0
       self.vel=0
       self.acc=0
       self.rot=0
       self.dyn=0

These need to be reset on each iteration of my program, what is the neatest way of doing this rather than setting to zero as shown above

Thanks

James Hopkin
  • 13,797
  • 1
  • 42
  • 71
mikip
  • 1,677
  • 6
  • 25
  • 35
  • 3
    turn them into a list, tuple, dict or some other sane data structure? – SilentGhost Jan 22 '10 at 09:03
  • 1
    Do you want to reset OBJECT attributes or CLASS attributes? You say class in your question, but seems by your code that you mean object attributes. – Khelben Jan 22 '10 at 09:31
  • 1
    As you see there are various ugly hacks to set all variables of an object to 0. I don't think any of them are the correct thing to do. The correct thing is more similar to what you are doing. But if you post the real code, we can help you in doing something sane instead. – Lennart Regebro Jan 22 '10 at 09:34
  • 1
    Why are you clearing an existing object? That seems completely wrong. – S.Lott Jan 22 '10 at 12:02
  • @Lennart Ugly hacks? Even hacks are pretty in Python ;-) – James Hopkin Jan 22 '10 at 16:34

5 Answers5

4

I'd rather not reset them in the init method but define a reset method for this, and call reset from init and before every subsequent iteration.

Johannes Charra
  • 29,455
  • 6
  • 42
  • 51
4

you can use vars() to return a dictionary of values that you can edit.

something like that

class A:
    def __init__(self):
        self.a=1
        self.b=1

    def reset(self):
        dic = vars(self)
        for i in dic.keys():
            dic[i] = 0

inst = A()
print inst.a , inst.b  # will print 1 1
inst.reset()
print inst.a , inst.b  # will print 0 0

if you dont want to edit some other attributes you can do something like that

def reset(self):
    dic = vars(self)
    noEdit = ['a']
    for i in dic.keys():
        if i not in noEdit:
            dic[i] = 0

this will not edit the variable a , although i think we have gone to far with this and we might be breaking an oop principle here :)

Ahmed Kotb
  • 6,269
  • 6
  • 33
  • 52
2
class MyClass(object):
  def __init__(self):
    attribs = 'time', 'pos', 'vel', 'acc', 'rot', 'dyn'
    vars(self).update((x, 0) for x in attribs)
James Hopkin
  • 13,797
  • 1
  • 42
  • 71
1

I had a similar question myself and had discovered that the best way to do it is just like Johannes Charra suggested:

class MyClass:

    def __init__(self):
        self.reset()

    def reset(self):
        self.acc = 0
        self.dyn = 0
        self.pos = 0
        self.rot = 0
        self.vel = 0
        self.time = 0

Now you have a reset() method which you can call on an instance of MyClass whenever you want to reset all those attributes.

Boštjan Mejak
  • 827
  • 9
  • 23
0

I'm not sure if this any more neat, but:

class MyClass:
    def __init__(self):
        for v in ('time', 'pos', 'vel', 'acc', 'rot', 'dyn'):
            exec("self.%s = 0" % v)

As SilentGhost suggested, you should probably put them in a sane data structure, such as tuple e.g.

class MyClass:
    def __init__(self):
        self.values = 20*(0,)

or, you can use dictionary:

class MyClass:
    def __init__(self):
        self.data = {}
        for v in ('time', 'pos', 'vel', 'acc', 'rot', 'dyn'):
        self.data[v] = 0
Kimvais
  • 38,306
  • 16
  • 108
  • 142
  • Thanks, the example I posted uses attributes of a,b,c,d,e. I use more descriptive names in my code e.g time, pos, vel, acc, rot, dyn etc. How could I reset these – mikip Jan 22 '10 at 09:32