Re: Dynamically defined functions via exec in imported module

2008-08-16 Thread Michele Simionato
On Aug 16, 4:02 pm, Maric Michaud <[EMAIL PROTECTED]> wrote: > I'd say that everywhere exec/eval are used in a application/function/lib that > doesn't mean to interpret arbitrary and user provided python code, it is a > bad usage Problem is, there are things you cannot do without exec/eval: for in

Re: Dynamically defined functions via exec in imported module

2008-08-16 Thread Michele Simionato
On Aug 16, 3:25 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > On Aug 16, 12:50 am, Michele Simionato <[EMAIL PROTECTED]> > wrote: > > 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 performa

Re: Dynamically defined functions via exec in imported module

2008-08-16 Thread Maric Michaud
Le Saturday 16 August 2008 06:50:02 Michele Simionato, vous avez écrit : > On Aug 16, 4:48 am, Nadeem <[EMAIL PROTECTED]> 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 teachin

Re: Dynamically defined functions via exec in imported module

2008-08-16 Thread Fredrik Lundh
Nadeem wrote: I understand that all this can be done with classes and OO programming, but the whole point of the HtDP curriculum is to introduce students to programming in a pedagogically-effective way using a functional approach instead of OO-first. And yet, one of the HtDP authors just poste

Re: Dynamically defined functions via exec in imported module

2008-08-16 Thread George Sakkis
On Aug 16, 12:50 am, Michele Simionato <[EMAIL PROTECTED]> wrote: > On Aug 16, 4:48 am, Nadeem <[EMAIL PROTECTED]> 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'

Re: Dynamically defined functions via exec in imported module

2008-08-15 Thread Michele Simionato
On Aug 16, 4:48 am, Nadeem <[EMAIL PROTECTED]> 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 al

Re: Dynamically defined functions via exec in imported module

2008-08-15 Thread Nadeem
That's a really neat way of doing it, thanks a lot! I hadn't realized how accessible all those globals() dictionaries were. Guess my example still falls in the 99%... :) --- nadeem > > def defineStruct(name, *parameters): >   class _struct: >     def __init__(self, *init_parameters): >       for

Re: Dynamically defined functions via exec in imported module

2008-08-15 Thread Calvin Spealman
On Fri, Aug 15, 2008 at 10:48 PM, Nadeem <[EMAIL PROTECTED]> 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 stud

Re: Dynamically defined functions via exec in imported module

2008-08-15 Thread Nadeem
Well, I found one hack that seems to achieve this by accessing the globals() dictionary of the outermost stack frame and adding an entry to it for the newly created functions: import inspect def dynamicdef(name, amt): '''Dynamically defines a new function with the given name that adds the

Re: Dynamically defined functions via exec in imported module

2008-08-15 Thread Nadeem
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 selecto

Re: Dynamically defined functions via exec in imported module

2008-08-15 Thread George Sakkis
On Aug 15, 7:26 pm, Nadeem <[EMAIL PROTECTED]> wrote: > Hello all, > I'm trying to write a function that will dynamically generate other > functions via exec. General tip: whenever you think you need to use exec (or eval), 99% of the time you don't; most of the time there is a better (meaning, le