$ cython --version
Cython version 0.25.2
$ python --version
Python 3.5.1
$ python setup.py build_ext -i
Error compiling Cython file:
------------------------------------------------------------
...
def say_hello_to(name):
print("Hello %s!" % name, end='')
^
------------------------------------------------------------
hello.pyx:2:33: Expected ')', found '='
$ cat hello.pyx
def say_hello_to(name):
print("Hello %s!" % name, end='')
print("Hello ", end='')
$ cat setup.py
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = 'hello',
ext_modules = cythonize("hello.pyx")
)
问题>如何在没有换行符的情况下在 Cython 中打印?
谢谢
手头没有 Cython 进行测试,但直接打印到 stdout 应该可以解决问题:
import sys
sys.stdout.write("Hello %s!" % name)
不想打印换行符?
在 py2 中:
print item, # the comma ',' does the trick.
在 py3 中:
print(item, end='')
Cython 编译默认为 py2 语法。如果使用仅在 py3 中有效的语法,那么在编译时,必须在安装程序中language_level=3
,或在命令行中cython -3
,或在 jupyter notebook 中%%cython -3
。
还有一件事,可以用cython编写py2语法,但链接到py3标头和库,这是将py2移植到py3的简单方法。