[Python-ideas] Classes inside functions [was Re: Add __parent__ to all classes, functions, and modules]

Steven D'Aprano steve at pearwood.info
Tue Oct 7 05:54:27 CEST 2014


On Mon, Oct 06, 2014 at 10:23:42AM -0700, Guido van Rossum wrote:

> How namedtuple is implemented should be nobody's business, except
> Raymond's, and it certainly isn't a pattern to be recommended. That's why
> it's in the stdlib -- so you don't have to write such code yourself. Same
> for enums. Yes, it *can* be done. But it takes superhuman skills to get it
> right and it still won't be maintainable (TBH, every time I see the
> namedtuple implementation I have to resist the urge to rewrite it. :-)

Heh :-)

I was curious to see how much code in namedtuple actually needed to be 
run through exec, so I forked Raymond's recipe on ActiveState, and 
re-wrote with the Inner class as a regular nested class, and only a 
two-line __new__ method needing to be run through exec.

http://code.activestate.com/recipes/578918-yet-another-namedtuple/

It passes the minimal tests in the recipe. I haven't taken it any 
further.


-- 
Steven


More information about the Python-ideas mailing list