Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64021
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'read.': 0.03; 'methods,': 0.09; 'override': 0.09; 'strings.': 0.09; 'python': 0.11; 'def': 0.12; 'times,': 0.14; "wouldn't": 0.14; 'behave': 0.16; 'devs': 0.16; 'dict': 0.16; 'does,': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'heap': 0.16; 'in-memory': 0.16; 'iterkeys': 0.16; 'libraries.': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'missing?': 0.16; 'personally,': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'simpson': 0.16; 'str()': 0.16; 'str):': 0.16; 'subclass': 0.16; 'subclasses.': 0.16; 'subclassing': 0.16; 'subject:subclasses': 0.16; 'substitution': 0.16; 'surprising': 0.16; 'wrote:': 0.18; 'implementing': 0.19; 'things.': 0.19; 'seems': 0.21; 'saying': 0.22; 'header:User- Agent:1': 0.23; 'fine': 0.24; 'cheers,': 0.24; 'question': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'idea': 0.28; 'raise': 0.29; 'tim': 0.29; 'code': 0.31; 'getting': 0.31; 'behaving': 0.31; 'bunch': 0.31; 'chase': 0.31; "d'aprano": 0.31; 'explained': 0.31; 'extensively': 0.31; 'steven': 0.31; 'violation': 0.31; 'class': 0.32; 'interface': 0.32; 'quite': 0.32; 'limitation': 0.33; 'programmers': 0.33; 'comment': 0.34; 'skip:_ 10': 0.34; 'core': 0.34; 'maybe': 0.34; 'basic': 0.35; "can't": 0.35; 'but': 0.35; 'there': 0.35; "he's": 0.36; 'received:com.au': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'too': 0.37; 'mapping': 0.38; 'received:211': 0.38; 'to:addr :python-list': 0.38; 'expect': 0.39; 'does': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'most': 0.60; 'content- disposition:inline': 0.62; 'more': 0.64; 'direct': 0.67; 'benefit': 0.68; 'stated': 0.69; 'special': 0.74; 'discovered': 0.83; 'dict,': 0.84; 'dict.': 0.84; 'elder': 0.84; 'amongst': 0.91; 'involved.': 0.91 |
| Date | Thu, 16 Jan 2014 08:42:42 +1100 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | python-list@python.org |
| Subject | Re: Chanelling Guido - dict subclasses |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Disposition | inline |
| In-Reply-To | <20140115050325.190b1509@bigbox.christie.dr> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| References | <20140115050325.190b1509@bigbox.christie.dr> |
| X-Optus-CM-Score | 0 |
| X-Optus-CM-Analysis | v=2.1 cv=bpB1Wiqi c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=onsusYo5PYwA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=8AHkEIZyAAAA:8 a=XwyQ2InafV4A:10 a=8uR6FKqcAAAA:8 a=EEH28JAt5EXwAk8lDDAA:9 a=KYAhlGzjx_9jhX6K:21 a=sA3gN_dQ5KFx4NQs:21 a=CjuIK1q_8ugA:10 |
| 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.5547.1389822175.18130.python-list@python.org> (permalink) |
| Lines | 59 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1389822175 news.xs4all.nl 2872 [2001:888:2000:d::a6]:46302 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:64021 |
Show key headers only | View raw
On 15Jan2014 05:03, Tim Chase <python.list@tim.thechases.com> wrote:
> On 2014-01-15 01:27, Steven D'Aprano wrote:
> > 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?
>
> Just as an observation, this seems almost exactly what anydbm does,
> behaving like a dict (whether it inherits from dict, or just
> duck-types like a dict), but with the limitation that keys/values need
> to be strings.
I would expect anydbm to be duck typing: just implementing the
mapping interface and directing the various methods directly to the
DBM libraries.
The comment in question was specificly about subclassing dict.
There is a rule of thumb amongst the core devs and elder Python
programmers that it is a bad idea to subclass the basic types which
I have seen stated many times, but not explained in depth.
Naively, I would have thought subclassing dict to constraint the
key types for some special purpose seems like a fine idea. You'd need
to override .update() as well and also the initialiser. Maybe it
is harder than it seems.
The other pitfall I can see is code that does an isinstance(..,
dict) check for some reason; having discovered that it has a dict
it may behave specially. Pickle? Who knows? Personally, if it is
using isinstance instead of a direct type() check then I think it
should expect to cope with subclasses.
I've subclassed str() a number of times, most extensively as a URL
object that is a str with a bunch of utility methods, and it seems
to work well.
I've subclassed dict a few times, most extensively as the in-memory
representation of record in a multibackend data store which I use
for a bunch of things. That is also working quite well.
The benefit of subclassing dict is getting a heap of methods like
iterkeys et al for free. A from-scratch mapping has a surprising
number of methods involved.
Cheers,
--
Cameron Simpson <cs@zip.com.au>
BTW, don't bother flaming me. I can't read.
- afdenis@lims03.lerc.nasa.gov (Stephen Dennison)
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Chanelling Guido - dict subclasses Cameron Simpson <cs@zip.com.au> - 2014-01-16 08:42 +1100
csiph-web