HELP: Weird attribute behavior...

Ralph Allan Rice rarice at core.com
Fri Jun 1 16:08:54 EDT 2001


Steven,

Yep, it looks like that was the reason.  I also found it in the
"Learning Python" book by Lutz & Ascher, page 158-159.  

Thanks,

Ralph


"Steven D. Majewski" wrote:
> 
> On Fri, 1 Jun 2001, Ralph Allan Rice wrote:
> 
> > I am developing a class that stores device reading information.  It
> > looks like this:
> >
> >
> > import string
> > import dbi, odbc
> >
> >
> > class Reading:
> >       __data  =  { }
> >
> >       def __init__(self, type, id, timezone, datestamp, value):
> >               self.__data['id'] = id
> >               self.__data['timezone'] = timezone
> >               self.__data['datestamp'] =datestamp
> >               self.__data['value'] = value
> >               self.__data['type']= type
> >
> >
> 
> change that to:
> 
>  class Reading:
>                                                 ## move __data
>         def __init__(self, type, id, timezone, datestamp, value):
>                 self.__data  =  { }             ## to here
>                 self.__data['id'] = id
>                 self.__data['timezone'] = timezone
>                 self.__data['datestamp'] =datestamp
>                 self.__data['value'] = value
>                 self.__data['type']= type
> 
> 
> In your example, __data is a shared class attribute.
> If you create it in the __init__ method, then it's owned by
> that instance only.
> 
> -- Steve Majewski



More information about the Python-list mailing list