Differences between revisions 1 and 37 (spanning 36 versions)
Revision 1 as of 2016-05-24 12:37:43
Size: 611
Editor: chapoton
Comment: creation
Revision 37 as of 2020-09-12 07:11:42
Size: 2829
Editor: chapoton
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Behaviour of print = ## page was renamed from PrintFunction

||<#FF6347>Warning: || Starting from version 9.0, the default distributed version of Sage is using Python 3. See [[Python3-Switch]] for more information.||

= Advice on writing Python3-compatible code =

It is now required that the code inside sage must be Python3 code. Let us describe a few important points.

== Do not use xrange ==

You can use xrange only inside Cython files (*.pyx).

== Do not use it.next() to advance iterators ==

Use instead the syntax "next(it)".

== Do not use .itervalues, .iteritems or .iterkeys ==

You can use .values, .items and .keys (that are not iterators in Python2).

== Do not use cmp() ==

The cmp global function is gone in Python3 and must be avoided completely.

SageMath now has a "richcmp" function that can sometimes replace "cmp".

== Behaviour of print ==
Line 5: Line 31:
In version 7.2, SageMath is using the Python 2 syntax. It may soon switch to the Python 3 syntax, where print is a function. SageMath code is written in Python3.
Line 7: Line 33:
To convert from python2 to python3, you basically need to add parentheses, and write print("x") instead of print "x". In the Python 3 syntax, print is a function.
Line 9: Line 35:
Here is a conversion table to help you adapt your code: The easiest way to achieve compatibility with Python3 is to always use '''print(...)''' with a single string inside the parentheses.
Line 11: Line 37:
|| Python 2 || Python 3 || For example, '''print("He has eaten {} bananas".format(10))''' will work in both Python 2 and Python 3 and give the same result.

Here below are some instructions for going from Python 2-only print to Python 3-only print.

||<#FFFF66>To convert '''print''' from Python 2 to Python 3, you basically need to add parentheses, and write '''print("x")''' instead of '''print "x"'''.||

Here is a conversion table to help you adapt your code in more complex cases:

||<#98FF98> Python 2 ||<#98FF98> Python 3 ||
|| print a || print(a) ||
Line 13: Line 48:
||print "%03d" % 7 || print("{:03d}".format(7)) ||
Line 16: Line 50:

== Author/Reviewer's checklist ==

Check the result of the patchbot Python3 compatibility plugins named "next_method", "oldstyle_print", "python3" and "raise_statements" (for example, see the list on the right at [[https://patchbot.sagemath.org/ticket/21996/|this patchbot report]]).

Otherwise, check Python 3 syntax errors in every file changed in the current branch:

{{{
git diff --name-only develop your-new-branch | xargs -n 1 python3 -m py_compile
}}}

Check Python 3 syntax errors in every Python file of Sage library (see this [[https://groups.google.com/d/msg/sage-devel/dwEABlLOWqI/NUNOARrLCAAJ|thread]]):

{{{
find src/sage -name '*.py' | xargs -n 1 python3 -m py_compile
}}}

If possible, include the following imports to the modules you are adding or working on:
{{{
from __future__ import division, absolute_import, print_function
}}}

The command pyflakes, when installed with pip3, is also very useful to detect incompatibilities with Python3.

Warning:

Starting from version 9.0, the default distributed version of Sage is using Python 3. See Python3-Switch for more information.

Advice on writing Python3-compatible code

It is now required that the code inside sage must be Python3 code. Let us describe a few important points.

Do not use xrange

You can use xrange only inside Cython files (*.pyx).

Do not use it.next() to advance iterators

Use instead the syntax "next(it)".

Do not use .itervalues, .iteritems or .iterkeys

You can use .values, .items and .keys (that are not iterators in Python2).

Do not use cmp()

The cmp global function is gone in Python3 and must be avoided completely.

SageMath now has a "richcmp" function that can sometimes replace "cmp".

Behaviour of print

The behaviour of print differs in Python 2 and in Python 3.

SageMath code is written in Python3.

In the Python 3 syntax, print is a function.

The easiest way to achieve compatibility with Python3 is to always use print(...) with a single string inside the parentheses.

For example, print("He has eaten {} bananas".format(10)) will work in both Python 2 and Python 3 and give the same result.

Here below are some instructions for going from Python 2-only print to Python 3-only print.

To convert print from Python 2 to Python 3, you basically need to add parentheses, and write print("x") instead of print "x".

Here is a conversion table to help you adapt your code in more complex cases:

Python 2

Python 3

print a

print(a)

print a, b, c

print(a, b, c)

print x,

print(x, end=" ")

print >>sys.stderr, x

print(x, file=sys.stderr)

Author/Reviewer's checklist

Check the result of the patchbot Python3 compatibility plugins named "next_method", "oldstyle_print", "python3" and "raise_statements" (for example, see the list on the right at this patchbot report).

Otherwise, check Python 3 syntax errors in every file changed in the current branch:

git diff --name-only develop your-new-branch | xargs -n 1 python3 -m py_compile

Check Python 3 syntax errors in every Python file of Sage library (see this thread):

find src/sage -name '*.py' | xargs -n 1 python3 -m py_compile 

If possible, include the following imports to the modules you are adding or working on:

from __future__ import division, absolute_import, print_function

The command pyflakes, when installed with pip3, is also very useful to detect incompatibilities with Python3.

Python3-compatible code (last edited 2020-09-12 07:11:42 by chapoton)