Re: run a string as code?



py_genetic wrote:
How can you make python interpret a string (of py code) as code. For
example if you want a py program to modify itself as it runs. I know
this is an advantage of interpreted languages, how is this done in
python. Thanks.

This might do it...

print eval.__doc__
eval(source[, globals[, locals]]) -> value

Evaluate the source in the context of globals and locals.
The source may be a string representing a Python expression
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mappping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.

.



Relevant Pages

  • Re: imported module no longer available
    ... would that remove it from the globals() and save it to a locals()? ... module.func# uses module from the global namespace ...
    (comp.lang.python)
  • Re: run a string as code?
    ... Evaluate the source in the context of globals and locals. ... new value of the state var which is a global var. ...
    (comp.lang.python)
  • Re: run a string as code?
    ... Evaluate the source in the context of globals and locals. ... new value of the state var which is a global var. ...
    (comp.lang.python)
  • Re: Halt execution procedure
    ... > deallocated on exit from a subprogram. ... > Fortran historically always did static allocation. ... It just seems like having STATIC locals could too easily waste ... Globals are variables that are not local, ...
    (comp.lang.fortran)
  • Re: run time memory address of my application
    ... All global and stack variables within your DSO? ... All globals and locals within entire app? ... All globals, locals and heap data within entire app? ...
    (comp.os.linux.development.apps)