Two practical cold tips on Python

Source: Internet
Author: User

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.

 
 
  1. #include <stdio.h>  
  2.  
  3. int main(int argc, char** argv)  
  4. {  
  5.         char* w = "hello" 
  6.                 " " 
  7.                 "world." 
  8.                 ;  
  9.         printf("%s", w);  
  10.         return 0;  
  11. }  

Output:

 
 
  1. hello world. 

Later, after writing Python for a long time, I realized that Python is actually okay:

 
 
  1. >>> t = ('hello' 
  2. ... ' ' 
  3. ... 'world')  
  4. >>> t  
  5. '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:

 
 
  1. If sys. version <'3 ':
  2. Exec ("chinese = unicode ('rayong hao', 'utf-8 ')")
  3. Else:
  4. Exec ("chinese = 'rayong hao '")

This is because in Python2.x

 
 
  1. Chinese = 'rayong hao'

The encoding is not unicode, but the prefix u of the string literal is canceled in Python3.x, so

 
 
  1. 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:

 
 
  1. >>> X = 'China'
  2. >>> X
  3. '\ Xe4 \ xb8 \ xad \ xe5 \ x9b \ xbd'
  4. >>> From _ future _ import unicode_literals
  5. >>> Y = 'China'
  6. >>> Y
  7. U' \ u4e2d \ u56fd'

In this way, my ugly code can also be beautified!

Original article: http://blog.csdn.net/lanphaday/article/details/6793567

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.