Chanelling Guido - dict subclasses

Peter Otten __peter__ at web.de
Wed Jan 15 03:40:33 EST 2014


Steven D'Aprano wrote:

> In the midst of that discussion, Guido van Rossum made a comment about
> subclassing dicts:
> 
>     [quote]

>     Personally I wouldn't add any words suggesting or referring
>     to the option of creation another class for this purpose. You
>     wouldn't recommend subclassing dict for constraining the
>     types of keys or values, would you?
>     [end quote]

> This surprises me, and rather than bother Python-Dev (where it will
> likely be lost in the noise, and certain will be off-topic), I'm hoping
> there may be someone here who is willing to attempt to channel GvR. I
> would have thought that subclassing dict for the purpose of constraining
> the type of keys or values would be precisely an excellent use of
> subclassing.
> 
> 
> class TextOnlyDict(dict):
>     def __setitem__(self, key, value):
>         if not isinstance(key, str):
>             raise TypeError

Personally I feel dirty whenever I write Python code that defeats duck-
typing -- so I would not /recommend/ any isinstance() check.
I realize that this is not an argument...

PS: I tried to read GvR's remark in context, but failed. It's about time to 
to revolt and temporarily install the FLUFL as our leader, long enough to 
revoke Guido's top-posting license, but not long enough to reintroduce the 
<> operator...




More information about the Python-list mailing list