Addition of dict_values, dict_keys, dict_items to keyword module

current scenario -
the keyword module does not mention

dict_values
dict_keys
dict_items

expected scenario -
they are mentioned either in __builtins__, or a new attribute, which could even include,

dict_valueiterator
dict_keyiterator
dict_itemiterator
dict_reverseitemiterator
dict_reveresevalueiterator
dict_reversekeyiterator
tuple_iterator
set_iterator
range_iterator
longrange_iterator
...

Why should they be mentioned as keywords when they are not keywords?

dict_values = 123
print(dict_values)
123

Note that the keyword module only includes python syntax, not all built-in objects. Thinks like if and for and while and def and class, and also True, False and None because those names are so important that they can’t be redefined.

Built-in objects like dict and list and range and enumerate belong to the builtins module, not the keyword module.

Regarding all of those iterators and views, I’m not sure there’s much use in exposing those as built-in names. If those appear in your code very often, I would take that as a bit of a code smell.

These are defined in Lib/_collections_abc.py:

# Private list of types that we want to register with the various ABCs
# so that they will pass tests like:
#       it = iter(somebytearray)
#       assert isinstance(it, Iterable)
# Note:  in other implementations, these types might not be distinct
# and they may have their own implementation specific types that
# are not included on this list.
bytes_iterator = type(iter(b''))
bytearray_iterator = type(iter(bytearray()))
#callable_iterator = ???
dict_keyiterator = type(iter({}.keys()))
dict_valueiterator = type(iter({}.values()))
...

Note the word “private”. They’re not for public use. Besides, as others pointed out, they’re not language keywords.

1 Like

I meant adding them to keyword.__builtins__ or some other attribute like keyword.inner_builtins
I do not mean to add them to keyword.kwlist

when I do import keyword; dir(keyword) then it gives __builtins__ as an attribute, so I am assuming builtins are also a part of keyword module.

this example would work for existing builtins also,

len = 123
print(len)

gives,

123

all of the ones I mentioned are PyTypeObject in the cpython repo, the same holds true for the existing builtins also, that is the main reason for me to suggest to add them to either keyword.__builtins__ or keyword.inner_builtins.

It appears that even collections._collections_abc has the __builtins__ attribute, so it is not only in keywords module.

on further inspection, I found out that the __builtins__ attribute is in a few more places, for example,

def f():
  pass
dir(f)

also includes __builtins__

And dir(collections._collections_abc) mentions the types I listed above.

but I think just like it has a __all__ list, there should be a list for the types mentioned in the op.

similar to how keyword module has a list for the keywords.

“Builtins” does not mean “keyword”.

When you do dir(keywords) you see __builtins__ as an entry because keywords is a module, and in CPython (but not other Python interpreters) all .py modules have an attribute __builtins__.

You should ignore __builtins__. It is not a public feature of modules, it is an implementation-detail not for public use, it is CPython-only, and not all modules have it.