SUMMARY
Sequences and mappings are both similar and different. Let's introduce and
use a new dunder attribute, to give item access appropriate behaviour. This
proposal is based on an earlier thread - see Acknowledgments for URL.

INTRODUCTION
In Python, there are two sorts of builtin objects that have item access.
They are mappings and sequences. They have different abstract base classes.
Mappings and sequences have fundamental similarities, and also fundamental
differences.

To see an example of this, let's consider
    >>> x = dict() # Mapping
    >>> y = [None] # Sequence

Consider now
    >>> x[0] = 'hi'
    >>> x[0] == 'hi'
    True
    >>> 0 in x, 'hi' in x
    (True, False)

Compare it with
    >>> y[0] = 'hi' # Similar
    >>> y[0] == 'hi'
    True
    >>> 0 in y, 'hi' in y # Different
    (False, True)

THE PROBLEM
Not taking into account the fundamental differences between mappings and
sequences can, I think, cause of difficulty when considering the semantics
of
    >>> z[1, 2, a=3, b=4]

If z is a sequence (or multi-dimensional array) then many of us would like
to think of item access as a function call. In other words, ideally,
    >>> z[1, 2, a=3, b=4]
    >>> z.__getitem__(1, 2, a=3, b=4)
are to be equivalent.

But if z is a mapping, then perhaps ideally we'd like an object
    >>> key = K(1, 2, a=3, b=4)
such that
    >>> z[1, 2, a=3, b=4]
    >>> z.__getitem__(key)
are equivalent.

PRESENT BEHAVIOUR
At present
    >>> z[1, 2, a=3, b=4]
roughly speaking calls
    >>> internal_get_function(z, 1, 2, a=3, b=4)
where we have something like
    def internal_get_function(self, *argv, **kwargs):
        if kwargs:
            raise SyntaxError
        if len(argv) == 1:
            key = argv[0]
        else:
            key = argv
        type(self).__getitem__(self, key)

PROPOSAL
I think it will help solve our problem, to give Z = type(z) a new dunder
attribute that either is used as the internal_get_function, or is used
inside a revised system-wide internal_get_function.

That way, depending on the new dunder attribute on Z = type(z), sometimes
    >>> z[1, 2, a=3, b=4]
    >>> z.__getitem__(1, 2, a=3, b=4)
are equivalent. And sometimes
    >>> z[1, 2, a=3, b=4]
is equivalent to
    >>> key = K(1, 2, a=3, b=4)
    >>> z.__getitem__(key)
all depending on the new dunder attribute on Z = type(z).

I hope this contribution helps.

ACKNOWLEDGEMENTS
I've had much valuable help from Ricky Teachey in preparing this message.
I've also been influenced by his and others contributions to an earlier
thread, which he started 3 weeks ago.
https://mail.python.org/archives/list/python-ideas@python.org/thread/FFXXO5NNUTMDKDAWIQS7JCHPA27Y7637/#FFXXO5NNUTMDKDAWIQS7JCHPA27Y7637

-- 
Jonathan
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IN5W3H2JLFNUCVRNC7I56ZOWACAG5OFW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to