Class static variables

musingattheruins at my-deja.com musingattheruins at my-deja.com
Fri Jul 21 13:15:40 EDT 2000


In article <8l9vb3$kae$1 at nnrp1.deja.com>,
  Guillaume <forums at memoire.com> wrote:
>
> > > I'm writing a tool to translate a variety of OO
> > > programming languages. Especially I target Python
> > > but I am hesitating about the way to define
> > > class-scope static variables. Could you suggest me
> > > what is the best and natural form ?
> > >
> > > Example: MyClass { static int a; }
> > > - MyClass_a
> > class MyClass:
> >     #define class attribute
> >     a = 0
> >     def method(self):
> >         #use class attribute
> >         do_something_with(MyClass.a)
> >
> > #use class attribute outside of MyClass
> > do_something_else_with(MyClass.a)
>
> Thanx for the answer but...
>
> >>> class CA:
> ...     a=0
> ...
> >>> b=CA()
> >>> b.a
> 0
> >>> b.a=2
> >>> c=CA()
> >>> c.a
> 0
> >>> b.a
> 2
>
> # c.a should be 2 and not 0 if a is a static attribute.

you are attaching a per-object variable with b.a=2 try CA.a - i think
you will find it is still 0.
regards,
bill


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list