I. I remember when I first started my work, the boss gave us the C ++ Basic Course and told us that the string literal volume can be wrapped (the following code). It felt like a dream.
- #include <stdio.h>
-
- int main(int argc, char** argv)
- {
- char* w = "hello"
- " "
- "world."
- ;
- printf("%s", w);
- return 0;
- }
Output:
- hello world.
Later, after writing Python for a long time, I realized that Python is actually okay:
- >>> t = ('hello'
- ... ' '
- ... 'world')
- >>> t
- 'hello world'
This feature is very useful and allows you to elegantly classify long code into several lines. I remember that when I used to spell SQL language and write log entries, I always felt tangled for the length of the code line exceeding 78 (see our programming specification: http://blog.csdn.net/lanphaday/article/details/6601123), and now there is no pressure.
Ii. When writing the test code of absolute32 (see: http://blog.csdn.net/lanphaday/article/details/6762023), to make the test code compatible with Python2.x/3.x, introduce the ugly code:
- If sys. version <'3 ':
- Exec ("chinese = unicode ('rayong hao', 'utf-8 ')")
- Else:
- Exec ("chinese = 'rayong hao '")
This is because in Python2.x
- Chinese = 'rayong hao'
The encoding is not unicode, but the prefix u of the string literal is canceled in Python3.x, so
- Chinese = u'rayong hao'
And directly syntax errors. At that time, I had to write exec code for compilation based on different versions. Later I learned that unicode_literals was introduced in Python2.6, which can easily write 2.x/ 3.x compatible code:
- >>> X = 'China'
- >>> X
- '\ Xe4 \ xb8 \ xad \ xe5 \ x9b \ xbd'
- >>> From _ future _ import unicode_literals
- >>> Y = 'China'
- >>> Y
- U' \ u4e2d \ u56fd'
In this way, my ugly code can also be beautified!
Original article: http://blog.csdn.net/lanphaday/article/details/6793567