eval

shindich at my-deja.com shindich at my-deja.com
Fri Jul 7 22:51:43 EDT 2000


In article
<Pine.LNX.4.21.0007070112170.6842-100000 at krispc6.physik.uni-karlsruhe.de>,
  Johannes Zellner <johannes at zellner.org> wrote:
> On Thu, 6 Jul 2000, Stuart Ford wrote:
>
> >
> > ----- Original Message -----
> > From: Johannes Zellner <johannes at zellner.org>
> > To: <python-list at python.org>
> > Sent: Thursday, July 06, 2000 5:46 PM
> > Subject: eval
> >
> >
> > >
> > > Hi,
> > >
> > > how does eval work ?
> > >
> > >     eval('print "fred"')
> > >
> >
> > exec not a function in the real sense, but a core command,
> > it does not require the use of parentheses.
> >
> > Examples:
> >
> > eval 'print "fred"'     will work..
> >
> > or
> >
> > command = 'print "fred"'
> > exec command                  will work just as well..
> >
> > I have used exec to process dynamically written code within the
> > current namespace.
>
> ahh. I see. thanks.
>
> And how do I define the scope where exec evaluates ?
> E.g.
>
> def generator(x):
>   eval 'def '+x+'(y): print "fred"'
>
> and after calling
> >>> generator('lola')
> I want to be able to use
> >>> lola('lolita')
>
> --
>    Johannes
>
You might want to try the following:
   >>> def generator (functionName, globaldict):
   ...     exec 'def ' + functionName + '(): print "fred"' in globaldict
   >>> generator ('lola', vars ())
   >>> lola
   <function lola at 80d4d08>
   >>> lola ()
   fred
   >>>

I hope this helps!

Alex Shindich
http://www.shindich.com/


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list