[Tutor] FrozenDict

Alexandre Chabot-Leclerc alex at alexchabot.net
Sat Oct 10 15:30:18 CEST 2015


> Aha, that's useful to know. So it's a no-no to subclass *any* builtin?

I don't think it's a no-no, I just think it comes with a few problems that are solved if you subclass the classes that are *meant* to be subclassed, like UserDict, UserList, or UserString.

> I checked collections.UserDict and it indeed looks promising. Caveat: it;s Python 3 only (not sure what versionit was introduced).

It's in both Python 2 and 3, but they're not in the same module. That's why I used the following construct to import it. In both cases, UserDict is imported correctly and the module where it came from does not matter.

try:
    from collections import UserDict
except ImportError:
    from UserDict import UserDict

Here are the docs:

- Python 2: https://docs.python.org/2/library/userdict.html
- Python 3: https://docs.python.org/3/library/collections.html#collections.UserDict

Regards,
   Alex


More information about the Tutor mailing list