ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

Dave Cinege dave at cinege.com
Tue Dec 11 23:39:12 CET 2012


On Tuesday 11 December 2012 16:53:12 Ian Kelly wrote:

> 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?

I'm finally beyond 2.2 and getting rid of 2.4 soon. Just started using 2.6 5 
months ago.

Thesaurus initially came about from me doing this:
class Global:
	pass
g = Global()

As a way to organize/consolidate global vars and eliminate the global 
statement.

After a brain fart one day I expanded this to some simple recursion and felt I 
was onto something as my entire life changed with how easy it now was to build 
output strings.

As you noted it was not possible to subclass dict, so I first tried with a 
class, and you run into recursion hell with __setatrib__ to which i think 
there is no fix.

I then made a UserDict version. Then when I moved mostly to 2.6 I could do a 
proper dict subclass. 

So I believe you're actually correct here.... 

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

Looks like an artifact from my UserDict version and was needed.  :-(

class UserDict:
    def __init__(self, dict=None, **kwargs):
        self.data = {}

Thanks for this. You'll see from the version number I wrote this 3 months ago 
so it's not 100% fresh in my mind. I'm releasing it now because a python coder 
I contracted to pick up some slack for me saw this and went ape at how much he 
liked it...and that prompted me to finally get it out into the wild.

As for in depth discussion and enhancement to this, I lack the time.

Dave



More information about the Python-list mailing list