ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Dec 12 19:30:38 EST 2012


On Wed, 12 Dec 2012 17:20:53 -0500, Dave Cinege wrote:

> Isn't super() depreciated? 

Heavens no. super() is the recommended way to do inheritance, and the 
*only* way to correctly do multiple inheritance[1]. What makes you think 
that it has been deprecated?


[...]
> To me for i in range(len(l)) seems like simpler, faster, tighter code
> for this now. 

It's not. It is more complex, slower, less direct code. Python is not C, 
or Fortran, or whatever low-level language you cut your teeth on and get 
your intuitions from.

[steve at ando ~]$ python -m timeit -s "L = list('abcdefghij')" "for i in 
range(len(L)):
    c = L[i]
    pass"
1000000 loops, best of 3: 1.67 usec per loop
[steve at ando ~]$ python -m timeit -s "L = list('abcdefghij')" "for i,c in 
enumerate(L):
    pass"
1000000 loops, best of 3: 1.39 usec per loop


That's only a small performance speedup, but the real advantages are:

* the version with enumerate is much more general: it works with 
  data structures where the length is expensive to calculate, lazy 
  data streams where the length is impossible to know up front, 
  and infinite data streams;

* the version with enumerate makes the intent more clear: since we
  care about looping over the items, we should iterate over the 
  items directly, not over their indices;

* it is more readable and maintainable: both loop variables (the
  index and the item) are defined in the same place, the start of 
  the for-loop, instead of one in the header and one in the body.



> What I'd really like to hear is that someone reading was curious enough
> to convert some existing code and their joy or displeasure with the
> experience.

I don't have any code with nested dicts where this would make a 
difference. If I had such code, I would be looking to redesign it so that 
I could avoided the nested dicts, not find a palliative. The Zen of 
Python is a useful guide to general design principles:

py> import this
The Zen of Python, by Tim Peters

[...]
Flat is better than nested.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.

Your Thesaurus class violates too many of these principles for it to be 
of interest to me. Having spent a good hour or so playing around with it 
in the interactive interpreter, it is too hard for me to reason about 
what it is doing (especially since your description of what it does is 
actively misleading), and too hard to predict under what circumstances it 
will fail.

Short code is not necessarily simple code, and I find your class too 
magical and complicated to be interested in using it in production code 
as it stands now.




[1] Well, technically there's another way: one might reimplement the 
functionality of super() in your own code, and avoid using super() while 
having all the usual joys of reinventing the wheel.


-- 
Steven



More information about the Python-list mailing list