ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

Cameron Simpson cs at zip.com.au
Tue Dec 11 23:31:22 CET 2012


On 11Dec2012 15:57, Dave Cinege <dave at 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 at zip.com.au>

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



More information about the Python-list mailing list