简体   繁体   中英

Unicode escaped comments in Python

I made a Java program that uses unicode escaped characters to break a multiline comment and hide some functionality. The program below prints "Hello Cruel World". I'm wondering if this is possible to do in Python (any version). If it is not possible, how is this prevented in the language?

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 + " ");
}

Note that the unicode is the escaped string */print("Cruel");/*

My fruitless attempt so far...

test = False

#\u0023test = True

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

print(test)

Python lexer only processes Unicode escapes within Unicode string literals ( u'' in Python 2, '' in Python 3), thus such an approach is not possible.

If you try just the simple space, \ , python spits out:

SyntaxError: unexpected character after line continuation character

That is because outside string literals, the \\ character is chiefly used in for breaking a long line into several shorter:

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

Outside strings, the only allowed character after \\ is a newline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM