Best way to control assignment to attribute?

Aahz aahz at pythoncraft.com
Wed Feb 4 15:01:43 EST 2004


Wes, please don't top-post.  Consider the following:

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?

In article <vXbUb.55174$6O4.1637541 at bgtnsc04-news.ops.worldnet.att.net>,
wes weston  <wweston at att.net> wrote:
>Aahz wrote:
>> In article <XF9Ub.175053$6y6.3406052 at bgtnsc05-news.ops.worldnet.att.net>,
>> wes weston  <wweston at att.net> wrote:
>>>Frank Millman wrote:
>>>>
>>>>I want to control the assignment of a value to an attribute. Instead
>>>>of allowing it to be changed directly, I want to enforce that a method
>>>>is called, which will perform the assignment subject to various
>>>>checks.
>>>
>>>   See __slots__; not sure if there's a newer/better way.
>> 
>> You should be certain before even thinking of suggesting __slots__.
>> __slots__ is intended only to save memory; there are many problems with
>> using it if you don't know what you're doing.
>
>He might be more interested in naming his class vars with two leading
>underscores. eh? This mangles the name making it not as it appears
>in the text and not accessible by the expressed name. Does not the
>__slots__ statement keep "you" from creating a new unintended class
>var?

Two leading underscores would be good, but it doesn't directly solve the
problem about controlling access to the attribute.  Yes, __slots__
prevents the creation of unintended attributes, but it also has other --
frequently undesirable -- consequences.  I encourage you to look up some
of the old threads in Google.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code -- 
not in reams of trivial code that bores the reader to death."  --GvR



More information about the Python-list mailing list