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


Groups > comp.lang.python > #63964

Re: Chanelling Guido - dict subclasses

From F <f@hop2it.be>
Newsgroups comp.lang.python
Subject Re: Chanelling Guido - dict subclasses
Date 2014-01-15 07:00 +0000
Organization A noiseless patient Spider
Message-ID <lb5bn0$2iq$1@dont-email.me> (permalink)
References <52d5e408$0$29970$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


I can't speak for Guido but I think it is messy and unnatural and will lead to user frustration.
 As a user, I would expect a dict to take any hashable as key and any object as value when using one. I would probably just provide a __getitem__ method in a normal class in your case. 

This said I have overriden dict before, but my child class only added to dict, I didn't change it's underlying behaviour so you can use my class(es) as a vanilla dict everywhere, which enforcing types would have destroyed.



On 01:27 15/01 , 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?
>
>
>-- 
>Steven 
>

-- 
yrNews Usenet Reader for iOS
http://appstore.com/yrNewsUsenetReader

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