'psyco' problem

P

Paulo da Silva

When running the following program:

#! /bin/env python
# -*- coding: iso-8859-15 -*-

import psyco
psyco.full()

def main():
n=eval("123+456")
print n

if __name__ == "__main__":
main()


I got:
../tp.py:7: warning: eval()/execfile() cannot see the locals in functions
bound by Psyco; consider using eval() in its two- or three-arguments form
def main():
579

What does this mean? Is there anything wrong?

Thank you.
 
A

Andrew Dalke

Paulo said:
> I got:
> ../tp.py:7: warning: eval()/execfile() cannot see the locals in functions
> bound by Psyco; consider using eval() in its two- or three-arguments form

In the following

def main():
n=eval("123+456")
print n

it appears that Psycho can't peer into the eval string
to figure out what's used. It doesn't know, for example,
if you're trying to do something like this

import math
def main():
n = 0
n=eval("123+n*math.pi")
print n

To handle it correctly it would need to understand
Python's full scopes, which is hard.

Instead, Psyco is asking you for some help. You
need to tell it what variables might be used inside
the eval string by passing the list of possible
values via the optional 2nd and 3rd arguments to
eval. See the docs for details. Here's how to make
what you want work

def main():
n=eval("123+456", {})
print n

WARNING: this is based solely on reading the error
message. My primary machine is a Mac and Psyco
doesn't run on it so I cannot test my answer.

Andrew
(e-mail address removed)
 
H

Hannu Kankaanp??

Paulo da Silva said:
When running the following program:

#! /bin/env python
# -*- coding: iso-8859-15 -*-

import psyco
psyco.full()

def main():
n=eval("123+456")
print n

if __name__ == "__main__":
main()


I got:
./tp.py:7: warning: eval()/execfile() cannot see the locals in functions
bound by Psyco; consider using eval() in its two- or three-arguments form
def main():
579

What does this mean? Is there anything wrong?

Thank you.

It means you can't do this if main() is bound by Psyco:

def main():
x=5
print eval("123+x")

because as the warning message says, Psycoed eval() cannot see the
locals (x here). And it suggest that you should consider using
eval() in its two- or three-arguments form, which is described
in the Python Library Reference. So with Psyco, you might use

y = 3
def main():
x=5
print eval("y+x", globals(), {'x':x})

But in your case, it of course doesn't matter because in
the expression 123+456 you aren't using any locals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,329
Latest member
InezZ76898

Latest Threads

Top