property question

Chris Liechti cliechti at gmx.net
Thu May 23 19:04:53 EDT 2002


Markus Jais <info at mjais.de> wrote in
news:acjrga$q04ag$1 at ID-75083.news.dfncis.de: 
> Chris Liechti wrote:
>> Markus Jais <info at mjais.de> wrote:
>> i think the usual way is to store the value in a variable with
>> preceeding underlines ("self._email" or two underlines if you want a
>> private attribute)
> 
> thanks for your tip: using an underline works
> but when I use two underlines with slots like this
> 
> class Address(object):
>         __slots__ = ('firstname', 'lastname', __email')
>         def __init__(self):
>                 self.__email         = "
> I get this error:
> AttributeError: 'Address' object has no attribute '_Address__email'

that's the way "private" attributes are created in python - by adding the 
class name in from of them.

never used __slots__ yet, but maybe it would work when you write:
  __slots__ = ('firstname', 'lastname', '_Address__email')

OTH making things private is not the prefered programming style in python. 
i would prefer one underline (that means by convention "that's meant to be 
internal, don't touch it", but it's still easy if you have to).

chris


-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list