Non-symmetric access

Jim Densmore (densmore@opus.dsaco.dsai.com)
Thu, 18 Aug 94 15:58:39 -0600

> > Here's a case where variable reference and variable assignments are
> > non-symmetric:
> >
> > class a: # give a unique integer ID to all instances
> > i = 1 # class variable
> > def __init__(self):
> > self.j = self.i # instance variable <-- class variable
> > self.i = self.i + 1 # modify class variable (?)
> >
> > Does the last statement modify the class variable or not?
> > No. Here's a sample run:
> >
> > >>> a1 = a() --> <instance...>
> > >>> a2 = a() --> <instance...>
> > >>> a1.__dict__ --> {j:1, i:2}
> > >>> a2.__dict__ --> {j:1, i:2}
> > >>> a.i --> 1
> >
> > Is this a good thing, or bad?
>
> Some call it a bug, others a feature. I call it a fact of Pythonic
> life :-)
> .
> .
> .

My vote is "feature", since:

>>> class c:
... i = 1
... def __init__(self):
... self.j = c.i
... c.i = c.i + 1
...

>>> a1 = c() --> <instance>
>>> a2 = c() --> <instance>
>>> a1.__dict__ --> {'j': 1}
>>> a2.__dict__ --> {'j': 2}
>>> c.i --> 3

________________________________________________________________
Jim Densmore, DECISION-SCIENCE APPLICATIONS Colorado Operations
1755 Telstar Drive, Suite 201, Colorado Springs, CO 80920-1018
Voice: 719/593-5974, Fax: -5978; Internet: densmore@dsai.com
Newsletter Editor, High Flights Soaring Club, Meadowlake Airport