Why does this work?

Delaney, Timothy tdelaney at avaya.com
Wed Mar 27 20:13:28 EST 2002


> From: Don Garrett [mailto:garrett at bgb-consulting.com]
> 
> "Delaney, Timothy" wrote:
> > 
> > __slots__
> 
>   Um... this does help protect them from outside 
> interferance, but doesn't in
> any way require them to have been initialized in __init__, or 
> even protect you
> from a misspelled name, does it? Just trying to make sure 
> that nothing goes on
> with __names besides the name mangling into __class__name.

http://www.amk.ca/python/2.2/index.html#SECTION000340000000000000000

__slots__ does indeed protect against misspelled names (but does not require
initialisation in __init__ - that's up to you).

For a private attribute, I think you would need to do the name mangling for
__slots__ manually i.e.

    __slots__ = ['_ClassName__attr']

although I'm happy to be corrected if this is not indeed the case :)

Tim Delaney




More information about the Python-list mailing list