Dynamically defined functions via exec in imported module

George Sakkis george.sakkis at gmail.com
Sat Aug 16 09:25:56 EDT 2008


On Aug 16, 12:50 am, Michele Simionato <michele.simion... at gmail.com>
wrote:
> On Aug 16, 4:48 am, Nadeem <nadeemabdulha... at gmail.com> wrote:
>
> > I understand the 99% rule... the example I gave was to simplify the
> > issue. The full thing I'm working on is a library for an introductory
> > CS class I'm teaching. I'm trying, essentially, to build a library of
> > macros for students to use allowing them to define records (like
> > structs in C) with selector functions.
>
> The namedtuple recipe by Raymond Hetting does
> exactly that and, guess what, it uses exec!

I might be wrong, but the reason namedtuple uses exec is performance.
IIRC earlier versions of the recipe used a metaclass instead, so it's
not that it *has* to use exec, it's just an optimization, totally
justified in this case since namedtuples should be almost as fast as
plain tuples.

George



More information about the Python-list mailing list