building embedded classes as in C/C++

Andy Beall beall at psych.ucsb.edu
Wed Aug 4 18:32:09 CEST 1999


Yes, thanks for the reply--I now see what I was doing wrong.  What
through me off (besides not reading thoroughly enough) was that non-list
and non-class variables are not class wide when declared using the
convention I did.  I understand now how to do what I want but why do
simple scalar-like variables work one way and lists and classes
another.  I'm definitely missing some subtlety of Python here...

For example,

class Object:
       p = 0 
       pos = Point()
       ang = Point()

Here, 'p' does not seem to be class-wide while 'pos' and 'ang' are.  Why
the discrepancy?

Thanks for any enlightenment,
Andy-



Markus Stenberg wrote:
> 
> Andy Beall <beall at psych.ucsb.edu> writes:
> > I'd like to build an embedded structure (Python class) much like I do in
> > C if possible.  When I do the following, multiple instances of my final
> > class overwrite each others sub-class values!
> 
> You did not pay attention, however; you set _class_wide_ variables, global
> to all instances (pos, ang). Thus, the behavior was to be expected.
> 
> Try this instead:
> 
> class Object:
>         def __init__(self):
>                 self.pos = Point()
>                 self.ang = Point()
> 
> >




More information about the Python-list mailing list