Ben Finney wrote:
> Alex Martelli <[EMAIL PROTECTED]> wrote:
>>Ben Finney <[EMAIL PROTECTED]> wrote:
>>
>>>How can a (user-defined) class ensure that its instances are
>>>immutable, like an int or a tuple, without inheriting from those
>>>types?
>>
>>You can make a good start by defining __setattr__, __delattr__ (and
>>__setitem__ and __delitem__ if your class is a container) to raise
>>exceptions.
>>Remember that your redefined __setattr__ IS "in place" even when
>>you're initializing your istance, so remember to delegate attribute
>>setting to the superclass (the other special methods mentioned above
>>are less likely to byte you).
> 
> So, for a class that needs to set attributes in __init__ (but after
> that, become immutable), how do I get around this? Should I make a
> _FooFunctionality class, and then inherit from that to make Foo as the
> immutable class that actually gets exported?

Typically, constants are set up in __new__ (which happens before
__init__), because by __init__ time the object is built.  Remember
not to complain that you have no copy operation, because there is no
such thing as a copy of a constant (the original constant is good
enough).

--Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to