2017-05-19 8 views
0
$ 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で改行なしの書式変数を印刷する方法

あなたは

+0

私は自分でテストしていませんが、それはバグのようです。 https://github.com/cython/cython/issues – DavidW

+1

で報告する価値があるかもしれません。さらに、Cythonのテストケースで 'end ='を使用しているようです:https://github.com/cython/cython/blob /a0bbb940c847dfe92cac446c8784c34c28c92836/tests/run/print_function.pyx。唯一の違いは、空の文字列ではなく空白を使用しているようだということです。 – DavidW

+0

@DavidW、あなたの提案に感謝します。修正は 'from __future__ import print_function'行をインクルードすることです。その他の変更は必要ありません。 – q0987

答えて

0

をテストするために手元にCythonをお持ちでないありがとう、しかしstdoutに直接印刷するには、トリックを行うべき:

import sys 

sys.stdout.write("Hello %s!" % name) 
0

は改行を印刷したくないですか? PY2で

:PY3で

print item, # the comma ',' does the trick. 

print(item, end='') 

CythonのコンパイルはPY2構文にデフォルトです。 py3で有効な構文が1つしかない場合、コンパイル時にはlanguage_level=3が、コマンドラインではcython -3、またはjupyterノートブックでは%%cython -3が明示的に指定されている必要があります。

さらにもう1つ、python2の構文をcythonで書くことができますが、py3ヘッダーとライブラリにリンクすると、py2をpy3に移植する簡単な方法です。

関連する問題