Ask Your Question
1

Syntax differences with python

asked 2017-09-10 06:53:42 +0200

oca gravatar image

updated 2017-09-10 13:09:08 +0200

Hello,

I'm trying to do a tricky function that requires a unknown number or regular argument, some default keywords argument and any number of other keyword argument.

In python, this code works:

def addplot_coordinates(*args, plus_option=False, **kwargs):
    pass

But it doesn't work with sage:

sage: def addplot_coordinates(*args, plus_option=False, **kwargs):
....:     pass
  File "<ipython-input-1-dace1b5103c9>", line 1
    def addplot_coordinates(*args, plus_option=False, **kwargs):
                                             ^
SyntaxError: invalid syntax

I naively though that any correct python code would work with sage. I found here* that there is some «minimal syntax difference», and searching «sage python syntax difference» on a search engine does not give relevant results.

Is there a document listing all the things someone coming from python should be careful about ? Can someone give me a pointer to such a document ?

Thank you for your time.

[*] I can't make a link, I meant this page /question/8528/sage-vs-pure-python/ on this forum.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2017-09-10 13:09:56 +0200

tmonteil gravatar image

This is not a Python vs Sage issue, but a Python2 vs Python3 issue (note that Sage still runs Python2):

With Python 2:

In [1]: def addplot_coordinates(*args, plus_option=False, **kwargs):
   ...:     pass
  File "<ipython-input-1-dace1b5103c9>", line 1
    def addplot_coordinates(*args, plus_option=False, **kwargs):
                                             ^
SyntaxError: invalid syntax

With Python 3:

In [1]: def addplot_coordinates(*args, plus_option=False, **kwargs):
   ...:     pass
   ...:

The workaround (both in Sage and Python2) is to use the following order:

sage: def addplot_coordinates(plus_option=False, *args, **kwargs):
....:     pass

In [2]: def addplot_coordinates(plus_option=False, *args, **kwargs):
   ...:     pass
edit flag offensive delete link more

Comments

It works indeed, thanks a lot !

I started with python3 so I don't know how it worked before.

Can someone confirm then that any correct python 2 code works with sage? And if not, I'm still looking for a document listing the differences.

oca gravatar imageoca ( 2017-09-10 13:32:10 +0200 )edit
2

The only difference is in the preparsing, see http://doc.sagemath.org/html/en/tutor...

tmonteil gravatar imagetmonteil ( 2017-09-10 13:36:10 +0200 )edit

Thank you !

oca gravatar imageoca ( 2017-09-11 05:27:41 +0200 )edit

Yes, python2.7 code works always through the sage interpreter. In an effort to make things easier for a mathematically educated programmer, and in order to put the right objects / classes / instances for doing maths without pain, there is also a pre-parsing step. For instance, 2^100 is converted to 2**100. And this will be an instance of "the integers",

sage: 2^100
1267650600228229401496703205376
sage: _.parent()
Integer Ring

Same for 100 / 82 which is not the python2.7 result, but exactly what i need for mathematical purposes

sage: 100 / 82
50/41
sage: _.parent()
Rational Field

In plain python2.7 one has

[dan@k7 ~]$ python2.7
>>> 100 / 82
1

(py3 gives 1.2195121951219512)

Always expect exact results, if possible.

dan_fulea gravatar imagedan_fulea ( 2017-09-11 21:37:42 +0200 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-09-10 06:51:30 +0200

Seen: 956 times

Last updated: Sep 10 '17