[Python-Dev] operator.is*Type (original) (raw)

Delaney, Timothy (Tim) tdelaney at avaya.com
Thu Feb 23 00:03:05 CET 2006


Raymond Hettinger wrote:

Your example simply highlights the consequences of one of Python's most basic, original design choices (using getitem for both sequences and mappings). That choice is now so fundamental to the language that it cannot possibly change.

Hmm - just a thought ...

Since we're adding the index magic method, why not have a getindexed method for sequences.

Then semantics of indexing operations would be something like:

if hasattr(obj, '__getindexed__'):
    return obj.__getindexed__(val.__index__())
else:
   return obj.__getitem__(val)

Similarly setindexed and delindexed.

This would allow distinguishing between sequences and mappings in a fairly backwards-compatible way. It would also enforce that only indexes can be used for sequences.

The backwards-incompatibility comes in when you have a type that implements getindexed, and a subclass that implements getitem e.g. if list implemented getindexed then any list subclass that overrode getitem would fail. However, I think we could make it 100% backwards-compatible for the builtin sequence types if they just had getindexed delegate to getitem. Effectively:

class list (object):

    def __getindexed__(self, index):
        return self.__getitem__(index)

Tim Delaney



More information about the Python-Dev mailing list