Skip to main content
 首页 » 编程设计

python之如何评论运算符之间的大赋值

2025年05月04日11shihaiming

fontName  = b"\xC8\xC1\x10" \ 
            # Representación del tipo de fuente en bytes. 
          + fontNamesInBytes[fontName] \ 
            # Tipo de atributo: attr_ubyte | Atributo: FontName (Nº 168) 
          + "\xF8\xA8" 

python3 test.py 
  File "test.py", line 14 
    + fontNamesInBytes[fontName] \ 
    ^ 
IndentationError: unexpected indent 
 
python3 test.py 
  File "test.py", line 13 
    \# Representación del tipo de fuente en bytes.\ 
                                                   ^ 
SyntaxError: unexpected character after line continuation character 
 
python3 test.py 
  File "test.py", line 15 
    """ Tipo de atributo: attr_ubyte | Atributo: FontName (Nº 168)"""\ 
                                                                     ^ 
SyntaxError: invalid syntax 

我可以在作业之间发表评论吗?我试过 """""" 但也给出了语法错误。

为 Martin 编辑:

fontName  = b"\xC8\xC1\x10" \ 
          """ Representación del tipo de fuente en bytes."""\ 
          + fontNamesInBytes[fontName] \ 
          """ Tipo de atributo: attr_ubyte | Atributo: FontName (Nº 168) """\ 
          + b"\xF8\xA8" 

    python3 test.py 
  File "test.py", line 15 
    """ Tipo de atributo: attr_ubyte | Atributo: FontName (Nº 168) """\ 
                                                                      ^ 
SyntaxError: invalid syntax 

请您参考如下方法:

你可以做到

In [5]: ('a' 
   ...: # comment 
   ...: 'b') 
Out[5]: 'ab' 

你的代码会变成

fontName  = (b"\xC8\xC1\x10"  
             # Representación del tipo de fuente en bytes. 
             + fontNamesInBytes[fontName] + 
             # Tipo de atributo: attr_ubyte | Atributo: FontName (Nº 168) 
             b"\xF8\xA8") 

这是有效的,因为括号、方括号或大括号中的表达式可以在不使用反斜杠的情况下拆分为多个物理行,隐式续行可以携带注释(根据到 documentation )。