Is this a contradiction in the docs ?

Fuzzyman fuzzyman at gmail.com
Wed Feb 2 09:14:08 EST 2005


Steve Holden wrote:
> Fuzzyman wrote:
>
> > The following two passages from the python documentation *appear*
to
> > contradict each other. Equally possible (or more likely !) is that
I
> > misunderstand it :
> >
> > eval :
> > This function can also be used to execute arbitrary code objects
(such
> > as those created by compile()). In this case pass a code object
instead
> > of a string. The code object must have been compiled passing 'eval'
as
> > the kind argument.
> >
> >
> > compile:
> > The kind argument specifies what kind of code must be compiled; it
can
> > be 'exec' if string consists of a sequence of statements, 'eval' if
it
> > consists of a single expression, or 'single' if it consists of a
single
> > interactive statement (in the latter case, expression statements
that
> > evaluate to something else than None will be printed).
> >
> > The docs for compile say that if you are creating a code object
from a
> > sequence of statements you must use the kind argument 'exec'. Eval
says
> > that if you are using the eval function you must use 'eval' as your
> > kind argument.
> >
> > In practise I have found that using the eval function with code
objects
> > compiled with 'exec' as the kind argument works fine. Is this a
'bug'
> > in the docs ?
> > Regards,
> >
>
> "Dear List:
>
> I live close to a railroad, and the tracks are surrounded by a fence
> with large signs saying 'Walking on the tracks can be dangerous'.
>
> I have walked on the tracks several times, however, and I don't see
why
> the railway company put those signs there. In fact I'm walking along
the
> tracks right now as I enter this post on my hand-held, and
>
> [message ends with sound of locomotive horn followed by an ugly
squelch]."
>
> That's the kind of practice best avoided. Are you breaking the rules
> just for the fun of it?
>

Hmm.... documentation on code objects says :

Code objects are returned by the built-in compile() function and can be
extracted from function objects through their func_code attribute.

A code object can be executed or evaluated by passing it (instead of a
source string) to the exec statement or the built-in eval() function.

It mentions no such restriction. I didn't properly absorb the 'must' in
the eval description......

Hmmm... my code will *never* need to run on anything other than CPython
because I use py2exe to turn it into a frozen python distribution. So
if it was always safe in CPython then that would be fine for me.

The reason I like eval is that "If both dictionaries are omitted, the
expression is executed in the environment where eval is called".
However I think exec might do the same thing anyway. I'll just switch
to exec.

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

> regards
>   Steve
> --
> Meet the Python developers and your c.l.py favorites March 23-25
> Come to PyCon DC 2005          http://www.python.org/pycon/2005/
> Steve Holden                           http://www.holdenweb.com/




More information about the Python-list mailing list