dant
dant

Reputation: 697

Python 2 import builtin

I need to make code compatible for Python2/3 and it uses the dict() builtin, but I can't figure out how to import it from the __future__ module.

For example,

dict.iteritems() does't exist in Python 3, and likewise dict.items() doesn't exist in Python 2.

Upvotes: 0

Views: 589

Answers (1)

BoarGules
BoarGules

Reputation: 16952

You can't do from __future__ import dict because Python 2 already has a dict. It is true that Python 2's dict.iteritems() does more or less what Python 3's dict.items() does.

But you are not correct when you say that Python 2 doesn't have dict.items(), and there is nothing to stop you using dict.items() in both versions. For all normal purposes they will behave the same.

Typically you use dict.items() like this:

for k,v in mydict.items():

That will work identically in both versions.

Upvotes: 1

Related Questions