[Python-ideas] Method chaining notation
David Mertz
mertz at gnosis.cx
Wed Feb 26 22:17:59 CET 2014
On Wed, Feb 26, 2014 at 12:54 PM, Ron Adam <ron3200 at gmail.com> wrote:
> For example you could say, we don't need dict.items because we can do...
>>
>
> zip(dict.keys(), dict.values())
>
Have we actually been promised that d.keys() and d.values() walk the
(unordered) dictionary in the same order, for every Python
implementation/version? While I think it is almost certainly true in
practice, I haven't where this invariant is guaranteed:
assert [d[k] for k in d] == d.values()
I could trivially subclass dict to make a pretty good dictionary that
violated this invariant, e.g.:
class SortedDict(dict):
def keys(self):
return sorted(dict.keys(self))
def values(self):
return sorted(dict.values(self))
I actually don't have great difficulty imagining purposes for which this
would be a useful data structure even. Clearly it violates the invariant
listed though.
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140226/9f7859a6/attachment-0001.html>
More information about the Python-ideas
mailing list