what is a dict_keys and where can I import it from?

Hi all, So, dicts in Python 3 return "something different" from their keys and values methods:
dict(x=1, y=2).keys() dict_keys(['y', 'x']) type(dict(x=1, y=2).keys()) <class 'dict_keys'>
I have vague memories of these things being referred to as views or some such? Where can I learn more? More importantly, how can I tell if I have one of them? I guess I can keep a reference to type({}.keys()) somewhere, but that feels a bit yucky. Should these things be in the types module? cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk

Is collections.KeysView good for you? On Tue, Feb 12, 2013 at 9:05 AM, Chris Withers <chris@simplistix.co.uk> wrote:
Hi all,
So, dicts in Python 3 return "something different" from their keys and values methods:
dict(x=1, y=2).keys() dict_keys(['y', 'x']) type(dict(x=1, y=2).keys()) <class 'dict_keys'>
I have vague memories of these things being referred to as views or some such? Where can I learn more?
More importantly, how can I tell if I have one of them? I guess I can keep a reference to type({}.keys()) somewhere, but that feels a bit yucky. Should these things be in the types module?
cheers,
Chris
-- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/andrew.svetlov%40gmail.com
-- Thanks, Andrew Svetlov

On 12/02/13 18:05, Chris Withers wrote:
Hi all,
So, dicts in Python 3 return "something different" from their keys and values methods:
dict(x=1, y=2).keys() dict_keys(['y', 'x']) type(dict(x=1, y=2).keys()) <class 'dict_keys'>
I have vague memories of these things being referred to as views or some such? Where can I learn more?
The Fine Manual is usually a good place to refresh your vague memories :-) http://docs.python.org/3/library/stdtypes.html#dict.keys By the way, they're also in Python 2.7, only they're called "viewkeys" instead.
More importantly, how can I tell if I have one of them?
Depends why you care. You may not care, but for those times where you do, they are in collections. py> from collections import KeysView py> keys = {}.keys() py> isinstance(keys, KeysView) True An anomaly, which I cannot explain: py> issubclass(type(keys), KeysView) True py> type(keys) is KeysView False py> type(keys).__mro__ (<class 'dict_keys'>, <class 'object'>) This disturbs my calm, because I expect that if issubclass returns True, the two classes will either be identical, or the second will be in the MRO of the first. What have I missed?
I guess I can keep a reference to type({}.keys()) somewhere, but that feels a bit yucky.
I remember Python 1.4 days when the only way to type-test something was: if type(something) is type([]): ... so dynamically grabbing the type from a literal when needed does not seem the least bit yucky to me. -- Steven

2013/2/12 Steven D'Aprano <steve@pearwood.info>
An anomaly, which I cannot explain:
py> issubclass(type(keys), KeysView) True py> type(keys) is KeysView False py> type(keys).__mro__ (<class 'dict_keys'>, <class 'object'>)
This disturbs my calm, because I expect that if issubclass returns True, the two classes will either be identical, or the second will be in the MRO of the first. What have I missed?
Ah, the magic of ABCs... KeysView overrides __instancecheck__, and can pretend to be any other class. This is precisely set in Lib/collections/abc.py: KeysView.register(dict_keys) -- Amaury Forgeot d'Arc
participants (4)
-
Amaury Forgeot d'Arc
-
Andrew Svetlov
-
Chris Withers
-
Steven D'Aprano