This is probably a stupid question, but what’s the best way to clear class variables between instances?
I know I could reset each variable individually in the constructor; but is there a way to do this in bulk?
Or am I doing something totally wrong that requires a different approach? Thanks for helping …
class User():
def __init__(self):
#RESET ALL CLASS VARIABLES
def commit(self):
#Commit variables to database
>>u = User()
>>u.name = 'Jason'
>>u.email = 'jason.mendez@yahoo.com.mx'
>>u.commit()
So that each time User is called the variables are fresh.
Thanks.
Can you just pass the parameters into the constructor like this?
There’s nothing to “reset” in the code you posted. Upon constructing a user, they don’t even have a name or email attribute until you set them later. An alternative would be to just initialize them to some default values as shown below, but the code I posted above is better so there won’t be any uninitialized User objects.