Basic Primer _python-modules and packages. Why Reload (SYS) before setdefaultencoding?

Source: Internet
Author: User

Brief description:

Note: When you reuse the import statement multiple times, the module is not reloaded, but the memory address of the module is referenced to the local environment variable

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import Public Module Imports systry:sys.setdefaultencoding (' Utf-8 ') except Exception, e:print e reload (SYS) sys.setd Efaultencoding (' Utf-8 ') print sys.getdefaultencoding () # Description: Import other modules if __name__ = = ' __main__ ': Pass


Reload:

Description: The module has been loaded to reload, generally used for the original module changes and other special cases, reload before the module must have been import, but it should be noted that the used instance will also use the old module, and the newly generated instance will use the new module, Reload after the original memory address

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import Public Module Imports systry:sys.setdefaultencoding (' Utf-8 ') except Exception, e:print e reload (SYS) sys.setd Efaultencoding (' Utf-8 ') print sys.getdefaultencoding () # Description: Import other modules if __name__ = = ' __main__ ': Pass

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/88/35/wKiom1frif2BKsmmAAEhOxOr39o402.png "title=" Del.png "alt=" Wkiom1frif2bksmmaaehoxor39o402.png "/>

Description: Many people do not name why reload () SYS is required to use setdefaultencoding to set the encoding, in fact, because the interpreter initialization is pre-executed/usr/lib64/python2.7/site.py, And in its 554 lines of code del sys.setdefaultencoding deleted this method, in fact, you import sys only points to the setdefaultencoding attribute of the SYS module address, So we need to reload again. Restore this method


This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1857444

Basic Primer _python-modules and packages. Why Reload (SYS) before setdefaultencoding?

Related Article

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.