Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev
slice subscripts for sequences and mappings
 

Index | Next | Previous | View Flat


eliben at gmail

Mar 3, 2012, 12:36 AM


Views: 478
Permalink
slice subscripts for sequences and mappings

Hello,

I find a strange discrepancy in Python with regards to slice
subscripting of objects, at the C API level. I mean things like
obj[start:end:step].

I'd expect slice subscripts to be part of the sequence interface, and
yet they are not. In fact, they are part of the mapping interface. For
example, the list object has its slice get/set methods assigned to a
PyMappingMethods struct. So does a bytes object, and pretty much every
other object that wants to support subscripts.

This doesn't align well with the documentation, in at least two places.

1) The library documentation
(http://docs.python.org/dev/library/stdtypes.html) in 4.8 says:

    "Mappings are mutable objects. There is currently only one
standard mapping type, the dictionary"

Why then does a list implement the mapping interface? Moreover, why
does bytes, an immutable object, implement the mapping interface?

2) The same documentation page in 4.6 says, in the operation table:

s[i:j] slice of s from i to j
s[i:j:k] slice of s from i to j with step k

But in the implementation, the slice subscripts are part of the
mapping, not the sequence inteface.

The PySequenceMethods structure does have fields for slice accessors,
but their naming (was_sq_slice, was_sq_ass_slice) suggests they're
just deprecated placeholders.

This also doesn't align well with logic, since mappings like dict have
no real meaning for slice subscripts. These logically belong to a
sequence. Moreover, it takes subscripts for single a single numeric
index away from subscripts for a slice into a different protocol (the
former in sequence, the latter in mapping).

I realize I must be missing some piece of the history here and not
suggesting to change anything. I do think that the documentation,
especially in the area of the type object that defines the sequence
and mapping protocols, could be clarified to express what is expected
of a new type that wants to act as a sequence. In particular, it
should be said explicitly that such a type must implement the mapping
protocol if it wants slice subscripting.

If this makes any sense at all, I will open an issue.

Eli
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Subject User Time
slice subscripts for sequences and mappings eliben at gmail Mar 3, 2012, 12:36 AM
    Re: slice subscripts for sequences and mappings eliben at gmail Mar 3, 2012, 12:50 AM
    Re: slice subscripts for sequences and mappings stefan_ml at behnel Mar 3, 2012, 1:24 AM
        Re: slice subscripts for sequences and mappings eliben at gmail Mar 3, 2012, 4:07 AM
    Re: slice subscripts for sequences and mappings solipsis at pitrou Mar 3, 2012, 4:20 AM
        Re: slice subscripts for sequences and mappings eliben at gmail Mar 3, 2012, 4:41 AM
        Re: slice subscripts for sequences and mappings guido at python Mar 3, 2012, 9:58 AM
    Re: slice subscripts for sequences and mappings solipsis at pitrou Mar 3, 2012, 4:48 AM
    Re: slice subscripts for sequences and mappings eliben at gmail Mar 3, 2012, 10:18 AM
    Re: slice subscripts for sequences and mappings thomas at python Mar 3, 2012, 12:59 PM
    Re: slice subscripts for sequences and mappings solipsis at pitrou Mar 3, 2012, 1:02 PM
        Re: slice subscripts for sequences and mappings thomas at python Mar 3, 2012, 6:20 PM
    Re: slice subscripts for sequences and mappings stefan_ml at behnel Mar 3, 2012, 1:12 PM
        Re: slice subscripts for sequences and mappings thomas at python Mar 3, 2012, 6:24 PM
    Re: slice subscripts for sequences and mappings eliben at gmail Mar 3, 2012, 7:37 PM
    Re: slice subscripts for sequences and mappings ncoghlan at gmail Mar 3, 2012, 7:59 PM
    Re: slice subscripts for sequences and mappings solipsis at pitrou Mar 4, 2012, 5:56 AM
    Re: slice subscripts for sequences and mappings greg.ewing at canterbury Mar 4, 2012, 1:14 PM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.