property problem

Daniel Fackrell dfackrell at DELETETHIS.linuxmail.org
Thu Jul 18 11:06:38 EDT 2002


You have defined __data as a private attribute:

"Peter Maas" <peter.maas at mplusr.de> wrote in message
news:3D36C65A.5040300 at mplusr.de...
> Hi,
>
> while experimenting with class properties using Python 2.2.1
> I encountered the following weird behaviour:
>
> This is my testclass:
>
> class proptest:
>
>   def __init__(self):
>   self.__data = 0

This declaration invokes python's name mangling.  The creation of x later
will call this constructor and the __data attribute will appear as
x._proptest__data (I think).

>   def get_data(self):
>   return self.__data
>
>   def set_data(self, value):
>   self.__data = value
>
>   data = property(get_data, set_data)
>
>   def meth(self):
>   return self.__data
>
> This is my testcode:
>
>  >>> x = proptest()
>  >>> x.data=234

This line creates a new attribute named x.data, which is completely
unrelated to x._proptest__data

>  >>> x.data
> 234
>  >>> x.meth()
> 0
>  >>> x.set_data(234)
>  >>> x.meth()
> 234

Does that help?

--
Daniel Fackrell (dfackrell at linuxmail.org)
When we attempt the impossible, we can experience true growth.





More information about the Python-list mailing list