Reset static variables or a workaround

N

Nav

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
 
C

Chris Rebert

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?

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
 
J

Jean-Michel Pichavant

Nav said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top