首页 文章

Unicode在Python中转义了注释

提问于
浏览
0

我创建了一个Java程序,它使用unicode转义字符来打破多行注释并隐藏一些功能 . 下面的程序打印出“Hello Cruel World” . 我想知道在Python(任何版本)中是否可以这样做 . 如果不可能,这种语言会如何阻止?

public static void main(String[] args) {
    print("Hello");

    /*
     * \u002A\u002F\u0070\u0072\u0069\u006E\u0074\u0028\u0022\u0043\u0072\u0075\u0065\u006C\u0022\u0029\u003B\u002F\u002A
     */
    print("World");
}

private static void print(String s){
    System.out.print(s + " ");
}

请注意,unicode是转义字符串 */print("Cruel");/*

到目前为止我没有成功的尝试......

test = False

#\u0023test = True

'''
\u0027\u0027\u0027
test = True 
\u0027\u0027\u0027
'''

print(test)

1 回答

  • 5

    Python词法分析器只处理Unicode转义_996111_ Unicode字符串文字(Python 2中的 u'' ,Python 3中的 '' ),因此这种方法是不可能的 .

    如果你只尝试简单的空间, \u0020 ,python吐出:

    SyntaxError: unexpected character after line continuation character
    

    这是因为外部字符串文字, \ 字符主要用于将长行分成几个较短的行:

    spam = foo + bar + baz + ham + \
           eggs + ham + spam + spam + \
           spam + sausages + spam
    

    外部字符串, \ 之后唯一允许的字符是换行符 .

相关问题