Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63956

Re: Chanelling Guido - dict subclasses

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'guido': 0.05; 'url:pipermail': 0.05; 'purpose.': 0.07; 'referring': 0.07; 'enormous': 0.09; 'override': 0.09; 'pep': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rossum': 0.09; 'def': 0.12; 'jan': 0.12; "wouldn't": 0.14; '460': 0.16; 'at,': 0.16; 'dict': 0.16; 'missing?': 0.16; 'multi-thread': 0.16; 'reboot': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'str):': 0.16; 'subclassing': 0.16; 'subject:subclasses': 0.16; 'substitution': 0.16; 'surprises': 0.16; 'wrote:': 0.18; 'discussion': 0.18; '(where': 0.19; '>>>': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'received:comcast.net': 0.24; "i've": 0.25; 'least': 0.26; 'certain': 0.27; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'words': 0.29; 'involving': 0.30; "i'm": 0.30; 'getting': 0.31; 'that.': 0.31; 'you?': 0.31; "d'aprano": 0.31; 'keys': 0.31; 'steven': 0.31; 'violation': 0.31; 'class': 0.32; 'option': 0.32; 'another': 0.32; 'url:python': 0.33; 'level.': 0.33; 'comment': 0.34; 'date:': 0.34; 'skip:_ 10': 0.34; 'problem': 0.35; 'problem.': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; "he's": 0.36; 'done': 0.36; 'url:org': 0.36; 'too': 0.37; 'two': 0.37; 'level': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'subject:': 0.39; 'channel': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'mailing': 0.39; 'received:org': 0.40; 'url:mail': 0.40; 'lost': 0.61; "you're": 0.61; 're:': 0.63; 'happen': 0.63; 'more': 0.64; 'talking': 0.65; 'here': 0.66; 'hoping': 0.75; '(oh': 0.84; '*really*': 0.84; '[python-dev]': 0.84; 'avoids': 0.84; 'dict,': 0.84; 'noise,': 0.84; 'pain': 0.84; 'python-dev': 0.84; 'url :python-dev': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Chanelling Guido - dict subclasses
Date Tue, 14 Jan 2014 21:04:22 -0500
References <52d5e408$0$29970$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <52d5e408$0$29970$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5489.1389751476.18130.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389751476 news.xs4all.nl 2862 [2001:888:2000:d::a6]:40181
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63956

Show key headers only | View raw


On 1/14/14 8:27 PM, Steven D'Aprano wrote:
> Over on the Python-Dev mailing list, there is an ENORMOUS multi-thread
> discussion involving at least two PEPs, about bytes/str compatibility.
> But I don't want to talk about that. (Oh gods, I *really* don't want to
> talk about that...)
>
> In the midst of that discussion, Guido van Rossum made a comment about
> subclassing dicts:
>
>      [quote]
>      From: Guido van Rossum <guido@python.org>
>      Date: Tue, 14 Jan 2014 12:06:32 -0800
>      Subject: Re: [Python-Dev] PEP 460 reboot
>
>      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]
>
> https://mail.python.org/pipermail/python-dev/2014-January/131537.html
>
> 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
>          super().__setitem__(key, value)
>      # need to override more methods too
>
>
> But reading Guido, I think he's saying that wouldn't be a good idea. I
> don't get it -- it's not a violation of the Liskov Substitution
> Principle, because it's more restrictive, not less. What am I missing?
>
>

One problem with it is that there are lots of ways of setting values in 
the dict, and they don't use your __setitem__:

 >>> tod = TextOnlyDict()
 >>> tod.update({1: "haha"})
 >>>

This is what you're getting at with your "need to override more methods 
too", but it turns out to be a pain to override enough methods.

I don't know if that is what Guido was getting at, I suspect he was 
talking at a more refined "principles of object design" level rather 
than "dicts don't happen to work that way" level.

Also, I've never done it, but I understand that deriving from 
collections.MutableMapping avoids this problem.

-- 
Ned Batchelder, http://nedbatchelder.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Chanelling Guido - dict subclasses Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-15 01:27 +0000
  Re: Chanelling Guido - dict subclasses Ned Batchelder <ned@nedbatchelder.com> - 2014-01-14 21:04 -0500
  Re: Chanelling Guido - dict subclasses Terry Reedy <tjreedy@udel.edu> - 2014-01-14 22:48 -0500
  Re: Chanelling Guido - dict subclasses F <f@hop2it.be> - 2014-01-15 07:00 +0000
  Re: Chanelling Guido - dict subclasses Peter Otten <__peter__@web.de> - 2014-01-15 09:40 +0100
    Re: Chanelling Guido - dict subclasses John Ladasky <john_ladasky@sbcglobal.net> - 2014-01-15 08:51 -0800
      Re: Chanelling Guido - dict subclasses Peter Otten <__peter__@web.de> - 2014-01-15 19:35 +0100
      Re: Chanelling Guido - dict subclasses Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-01-15 22:30 -0800
  Re: Chanelling Guido - dict subclasses Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-15 09:10 +0000
  Re: Chanelling Guido - dict subclasses Tim Chase <python.list@tim.thechases.com> - 2014-01-15 05:03 -0600
  Re: Chanelling Guido - dict subclasses Daniel da Silva <var.mail.daniel@gmail.com> - 2014-01-15 19:50 -0500
    Re: Chanelling Guido - dict subclasses Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-16 17:17 +1300

csiph-web