Reset static variables or a workaround
Jean-Michel Pichavant
jeanmichel at sequans.com
Thu Feb 23 05:18:07 EST 2012
Nav wrote:
> Hi Guys,
>
> I have a custom user form class, it inherits my own custom Form class:
>
> class UserForm(Form):
> first_name = TextField(attributes={id='id_firstname'})
>
> Now, everytime UserForm() is instantiated it saves the attributes of
> each form members and passes it on to the new instance.
I'm not sure I've understood this sentence but if you're saying that
class attributes are copied into the subclass instance, that's wrong.
> I understand
> this is because first_name is static in nature. But I would like to
> reset the first_name for every instance? How can I do this?
>
> Regards,
> Nav
>
Class attributes are not default values for instances.
If you want to set the first_name attribute for every instances, you
have to make it an instance attribute:
class Form:
def __init__(self):
self.first_name = "foo"
class UserForm(Form):
def __init__(self, name):
Form.__init__(self)
self.first_name = name
uForm = UserForm('banana')
print uForm.first_name
Cheers,
JM
More information about the Python-list
mailing list