Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reset static variables or a workaround

5 views
Skip to first unread message

Nav

unread,
Feb 23, 2012, 4:26:21 AM2/23/12
to
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 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

Chris Rebert

unread,
Feb 23, 2012, 4:43:42 AM2/23/12
to Nav, pytho...@python.org
I infer that your question concerns Django. You might want to ask on
their discussion group instead:
http://groups.google.com/group/django-users

Cheers,
Chris

Jean-Michel Pichavant

unread,
Feb 23, 2012, 5:18:07 AM2/23/12
to Nav, pytho...@python.org
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

0 new messages