Ask Your Question
1

exec(func()) not the same as func() ?

asked 2016-06-15 18:37:24 +0100

seriousstuff gravatar image

When I am running this:

 poly=Polyhedron(vertices=[(1, 0, 0, 0), (4752/8677, 7260/8677, 0, -35/8677), (147875/208902, 41405/69634, 79625/208902, 1877/208902), (1514240/2130249, 927472/2130249, 163072/710083, 1070281/2130249), (360/869, 300/869, 600/869, 419/869), (63000/183661, 123480/183661, 73500/183661, 95461/183661), (1871100/3093289, 1568160/3093289, 1524600/3093289, 1133089/3093289), (77175/215867, 113400/215867, 119070/215867, 116642/215867), (169000/263949, 41600/87983, 13520/37707, 128749/263949)],base_ring=QQ)
print(poly.vertices())

I get the expected result. However, if I run this

exec('poly=Polyhedron(vertices=[(1, 0, 0, 0), (4752/8677, 7260/8677, 0, -35/8677), (147875/208902, 41405/69634, 79625/208902, 1877/208902), (1514240/2130249, 927472/2130249, 163072/710083, 1070281/2130249), (360/869, 300/869, 600/869, 419/869), (63000/183661, 123480/183661, 73500/183661, 95461/183661), (1871100/3093289, 1568160/3093289, 1524600/3093289, 1133089/3093289), (77175/215867, 113400/215867, 119070/215867, 116642/215867), (169000/263949, 41600/87983, 13520/37707, 128749/263949)],base_ring=QQ)')
print(poly.vertices())

I get something completely different. Why is that and how can I fix this? I have to use the exec-command, because I want to read the vertices from a file.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-06-16 01:56:46 +0100

dgulotta gravatar image

The exec() function interprets its argument as pure Python code rather than Sage code. In Python 2, the code '4752/8677' rounds the ratio down to the nearest integer.

In any case, I would not recommend using the exec() command to read data from a file, as it could be a security risk and it may give confusing error messages if the data are not in the format that you expect. You might consider using pickle/csv/json instead.

edit flag offensive delete link more
0

answered 2016-06-16 02:11:09 +0100

updated 2016-06-16 03:53:00 +0100

If you choose to use exec, you can get the desired behavior by adding this line before it:

from __future__ import division

That uses Python 3 to treat the ratio of two integers as true division. More information:

http://stackoverflow.com/questions/57...

https://www.python.org/dev/peps/pep-0...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-06-15 18:37:24 +0100

Seen: 364 times

Last updated: Jun 16 '16