簡體   English   中英

在Cython中連接字符串時的語法錯誤

[英]Syntax error when concatenating a string in Cython

我在cdef類(Cython語言)中包含以下代碼:

def toString(self):
    res = "lut= " + str(self._mm_np[0].lut) +
        "n1= "  + str(self._mm_np[0].n1) +
        "nlay= "+ str(self._mm_np[0].nlay) +
        "n3= "  + str(self._mm_np[0].n3)
    return res

當我嘗試編譯包含此代碼的cython文件時,出現以下語法錯誤:“期望標識符或文字”指向字符串連接中第一個“ +”的位置。

我嘗試使用'\\'代替'+'失敗。.在Pyhton / Cython中連接字符串的正確方法是什么? 謝謝!

您缺少行繼續運算符\\

def toString(self):
    res = "lut= " + str(self._mm_np[0].lut) + \
    "n1= "  + str(self._mm_np[0].n1) + \
    "nlay= "+ str(self._mm_np[0].nlay) + \
    "n3= "  + str(self._mm_np[0].n3)
    return res

...但是您確實不應該這樣做。 被認為是差勁的風格。

探索.format方法用於字符串的用法; 它將提供位置參數到字符串,這樣你就不必來連接。

def toString(self): 
    return "lut={} n1={} nlay={} n3={}".format(
                str(self._mm_np[0].lut),
                str(self._mm_np[0].n1),
                str(self._mm_np[0].nlay),
                str(self._mm_np[0].n3))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM