Code comment for Python #-*-Coding:utf-8-*-

Source: Internet
Author: User

Go

#-*-Coding:utf-8-*-The main role is to specify the file encoding for Utf-8, because the general default is the ASCII code, if you want to write Chinese in the file, the runtime will appear garbled, plus this sentence will be converted to utf-8 the file code will not appear garbled.

Declarative syntax reference for Python pep http://www.python.org/dev/peps/pep-0263/

The main contents are as follows:

1. The code note must be placed on the first or second line

2. The optional format is

# coding=<encoding Name>  
#-*-Coding: <encoding name>-*-  
# Vim:set fileencoding=<encoding name>:  

3. The standard gives a valid string as long as it satisfies the following regular expression:

\%^.*\n.? \?#.*coding[:=]\s*[0-9a-za-z-_.] \+.*$  

The meaning of this is that # must be included, and the # number cannot be preceded by other characters such as character wrapping, and the string must contain coding followed by: or = Next is the encoded name.

But why is this usually the way it is?

#-*-Coding: <encoding name>-*-  

The answer is mentioned in PEP-0263, that is, Emacs and other editors use this way to encode the declaration. This writing can support a variety of editors, good portability.

Reference Documentation:

http://www.python.org/dev/peps/pep-0263/

http://blog.csdn.net/arbel/article/details/7957782

Code comment for Python #-*-Coding:utf-8-*-

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.