Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20717 > unrolled thread

Reset static variables or a workaround

Started byNav <navkirats@gmail.com>
First post2012-02-23 01:26 -0800
Last post2012-02-23 11:18 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Reset static variables or a workaround Nav <navkirats@gmail.com> - 2012-02-23 01:26 -0800
    Re: Reset static variables or a workaround Chris Rebert <clp2@rebertia.com> - 2012-02-23 01:43 -0800
    Re: Reset static variables or a workaround Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-02-23 11:18 +0100

#20717 — Reset static variables or a workaround

FromNav <navkirats@gmail.com>
Date2012-02-23 01:26 -0800
SubjectReset static variables or a workaround
Message-ID<9a1910c3-ccb0-493a-9756-4c6264e7c3b6@o4g2000pbc.googlegroups.com>
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

[toc] | [next] | [standalone]


#20718

FromChris Rebert <clp2@rebertia.com>
Date2012-02-23 01:43 -0800
Message-ID<mailman.68.1329990223.3037.python-list@python.org>
In reply to#20717
On Thu, Feb 23, 2012 at 1:26 AM, Nav <navkirats@gmail.com> 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 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

[toc] | [prev] | [next] | [standalone]


#20719

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-02-23 11:18 +0100
Message-ID<mailman.69.1329992297.3037.python-list@python.org>
In reply to#20717
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web