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


Groups > comp.lang.python > #34657

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

Date 2012-12-12 09:31 +1100
From Cameron Simpson <cs@zip.com.au>
Subject Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes
References <201212111557.24851.dave@linkscape.net>
Newsgroups comp.lang.python
Message-ID <mailman.750.1355265569.29569.python-list@python.org> (permalink)

Show all headers | View raw


On 11Dec2012 15:57, Dave Cinege <dave@linkscape.net> wrote:
| On Tuesday 11 December 2012 01:41:38 Ian Kelly wrote:
| > running into bugs like this:
| > >>> thes = Thesaurus()
| > >>> thes.update = 'now'
| > >>> thes.update
| > 
| > <built-in method update of Thesaurus object at 0x01DB30C8>
| 
| I've noticed this but it's mostly pointless, as meaningful code does work.
| (Also you stepped on the existing 'update()' dictionary method.)

I think that was a deliberate choice of name by Ian.

I've got a class like Thesaurus that subclasses dict and maps attributes
to dictionary elements (with a few special purpose tweaks I could go into
if anyone cares). I made a deliberate decision to only map UPPERCASE
attributes to dict keys to avoid exactly the kind of conflict above,
because:

  thes.update = 'now'

must either trash the dict.update method _or_ fail to present .update as
'now'. Both have their downsides. So at the cost of shoutier but still
effective code I accepted only .UPPERCASE attribute names as mapping to
keys.

This compromise also makes subclassing much easier, because the
subclasser is free to use conventional lowercase attribute names.

Cheers,
-- 
Cameron Simpson <cs@zip.com.au>

Thousands at his bidding speed,
And post o'er land and ocean without rest       - Milton

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


Thread

Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Cameron Simpson <cs@zip.com.au> - 2012-12-12 09:31 +1100

csiph-web