[Python-Dev] Re: A proposal has surfaced on comp.lang.pythontoredefine"is"

Bob Ippolito bob at redivi.com
Fri Mar 19 16:24:01 EST 2004


On Mar 19, 2004, at 3:03 PM, Andrew Koenig wrote:

>> Until someone writes, tests, and publishes an equiv function, and 
>> others
>> verify its usefulness, that strikes me as a speculative hypothesis.
>> Concrete code would also be clearer to me than the natural language
>> definitions I have seen.
>
> I would do so if I knew an easy way to determine whether an object is
> mutable.

If something like PEP 246 (Object Adaptation) were adopted with an 
implementation such as PyProtocols, it would be rather easy to tag 
types or objects as mutable or immutable.

untested example


# mutability.py
#
import protocols

__all__ = ['IImmutable', 'is_mutable']

class IImmutable(protocols.Interface):
     pass

def is_mutable(obj, _mutable_sentinel = object()):
     return protocols.adapt(obj, IImmutable, default=_mutable_sentinel) 
is _mutable_sentinel

def _setup():
     # don't pollute the module namespace
     for typ in (int, long, str, unicode, frozenset):
         protocols.declareImplementation(typ, 
instancesProvide=IImmutable)
_setup()



Of course, with this particular implementation, new immutable types 
would declare their immutability explicitly, and subtypes of immutable 
types that are now mutable would explicitly declare that they do not 
provide immutability.  Also note that something like this requires no 
changes to existing code at all.

-bob




More information about the Python-Dev mailing list