views:

74

answers:

4

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 = '[email protected]'
>>u.commit()

So that each time User is called the variables are fresh.

Thanks.

+1  A: 

Can you just pass the parameters into the constructor like this?

class User(object):
    def __init__(self, name, email):
        self.name = name
        self.email = email
    def commit(self):
        pass

jason = User('jason', '[email protected]')
jack = User('jack', '[email protected]')

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.

def __init__(self):
    self.user = None
    self.email = None
FogleBird
This is the right way do implement a class like this—passing in the values, not assigning the attributes manually.
Mike Graham
Your edit points out a technique that is suboptimal. =(
Mike Graham
@Mike Graham, it is suboptimal, and that's why I said the other way is better. But you don't always pass everything into the constructor so I thought it was worth mentioning.
FogleBird
A: 

Binding an attribute on an instance creates instance attributes, not class attributes. Perhaps you are seeing another problem that is not shown in the code above.

Ignacio Vazquez-Abrams
A: 

This code does not change the name or email attributes of any of the instances of User except for u.

Mike Graham
+2  A: 

If you want to reset the values each time you construct a new object then you should be using instance variables, not class variables.

If you use class variables and try to create more than one user object at the same time then one will overwrite the other's changes.

Mark Byers
Note that the current snippet isn't using class attributes; it's using instance attributes. Your last statement is quite incorrect.
Mike Graham
@Mike Graham: I don't think his snippet is complete (as it is, it won't even compile). Read the comments in the \_\_init\_\_ method - he has omitted some code which uses class variables. Nevertheless I have tried to clarify my comment.
Mark Byers
+1: "clearing class variables" makes no sense, since that's what instance variables **are** in the first place: variables which are created fresh with each instance.
S.Lott
@Mark Byers, setting `u.name = 'Jason'` and so forth will not set class attributes of `User` even if there is a class attribute of the same name. (Unless there is some magic involved.) I think he thinks this is what deals with the "class variables", by which he may mean instance attributes, class attributes, or not know what he means.
Mike Graham
@Mike Graham: True, but also there may have been other variables involved that were class variables but had been omitted from the snippet. There were two ways to interpret the question, and it was impossible to say who was "wrong". Either way, in my opinion any answer that doesn't address the point about class and instance variables is an incomplete answer. The OP is clearly confused and regardless of whether my interpretation was correct I still think it was worth pointing this out to hopefully clarify things for the OP.
Mark Byers
Pylons uses a multi-threaded application server and class variables are not cleared from request to request. I should have been more clear that this was pylons-specific. Regardless, this issue is resolved. Thanks.
ensnare