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


Groups > comp.lang.python > #34650 > unrolled thread

Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2012-12-11 14:53 -0700
Last post2012-12-11 14:53 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Ian Kelly <ian.g.kelly@gmail.com> - 2012-12-11 14:53 -0700

#34650 — Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-12-11 14:53 -0700
SubjectRe: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes
Message-ID<mailman.746.1355262824.29569.python-list@python.org>
On Tue, Dec 11, 2012 at 1:57 PM, Dave Cinege <dave@linkscape.net> wrote:
> On Tuesday 11 December 2012 01:41:38 Ian Kelly wrote:
>> Second, in __getitem__ you start a loop with "for i in
>> range(len(l)):", and then you use i as an index into l several times.
>> It would be cleaner and more Pythonic to do "for i, part in
>> enumerate(l):", and then you can replace every occurrence of "l[i]"
>
> My python is still 'old school' due to being stuck on old versions for in
> production embedded system python applications.

Just out of curiosity, how old are we talking?  enumerate was added in
Python 2.3, which is nearly 10 years old.  Prior to 2.2 I don't think
it was even possible to subclass dict, which would make your Thesaurus
implementation unusable, so are these systems running Python 2.2?

>> It's not clear to me what the isinstance call here is meant to be
>> testing for.
>
> It's used to determine if it's the root instance of the recursive string
> because self.data, not self must be used to access that. Can you offer a better
> way?
>
>> The prior statements require key to be a string.  If key
>> is a string, then by construction l[0] is also a string.  So it seems
>> to me that the isinstance check here will always be False.
>
> OK, try and remove it and then get back to me. :-)

Okay.  I replaced this code:

    if isinstance(l[0], (dict, Thesaurus)):
        a = self.data
    else:
        a = self

with:

    a = self

and then I ran the examples, and the output was unchanged.  As Steven
pointed out, I don't see how that first branch could succeed anyway,
since self.data is never defined.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web