Issue
I'm upgrading Python2 code to by compatible with Python3. As part of the process I'm using the future package to import Python3 behaviour into Python2.
When doing this, the dict.keys() method returns a Python3 view object rather than a list, so no more need to call dict.view_keys(). However, if the dictionary is created with {}, the Python2 behaviour is retained :
>>> from builtins import *
>>> type({'a':1, 'b':2}.keys())
<type 'list'>
>>> type(dict(a=1, b=2).keys())
<type 'dict_keys'>
Is there a way to bind {} to the new dict() class?
Solution
I'm afraid that {}
are unalterably linked to the interpreter's notion of dictionary (and set).
On the other hand, dict
is just a name in the global namespace, and can be easily replaced; this is what your library does. It does not replace the implementation of the built-in dictionary, it just adds its own implementation and makes dict()
to point to it.
I'm afraid there's no way to comprehensively replace the dictionary implementation, including things like **kwargs
, and numerous parts of the standard library that use {}
-syntax.
Answered By - 9000
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.