Reputation: 192
I am looking for a way to recreate / reinstantiate an object in python, because I want the object to have a default attribute in each loop.
for case in all_case:
# I want an object to be newly created / reinstantiated in each loop
pda = PushDownAutomata()
print pda.evaluate(case, debug=False)
# I already added "del pda" but it does not work
How can I achieve it?
Upvotes: 0
Views: 2334
Reputation: 192
The problem was fixed by the following code. I was using a class variable before - changing it to an instance variable solved the issue:
# before (class variable)
class PushDownAutomata():
stack = []
state = 'q1'
# after (instance variable)
class PushDownAutomata():
def __init__():
self.stack = []
self.state = 'q1'
Upvotes: 1
Reputation: 1085
Note that class attributes are not the same as instance attributes. for example:
class A(object):
a = None # this is a class attribute
def __init__(self, b):
self.b = b # b is an instance attribute
Upvotes: 3