Reasons why a command cannot be executed properly after the Python IDLE reload (SYS)

Source: Internet
Author: User

Tag: Copy encounters standard obj starting reliable work code track

Reprinted from: Http://blog.csdn.net/kxcfzyk/article/details/41414247?utm_source=tuicool&utm_medium=referral

Most people usually execute the reload (SYS) statement just to be able to modify the Python default character set, which is the ability to call sys.setdefaultencoding (). However, if you execute reload (SYS)in idle, you will cause the next failure to execute any commands.

At first there was nothing to do with the problem, and later on StackOverflow it was seen that someone had said it. This is because idle, as a GUI shell environment, sets specific standard input, standard output, and standard error output during startup initialization, so that both the input and output are in the idle GUI shell and can be viewed in idle in the following way:

[Python]View PlainCopy
    1. >>> Import sys
    2. >>> Print Sys.stdin
    3. <idlelib. Pyshell.pseudoinputfile object at 0x00000000027af2e8>
    4. >>> Print Sys.stdout
    5. <idlelib. Pyshell.pseudooutputfile object at 0x00000000027af320>
    6. >>> Print Sys.stderr
    7. <idlelib. Pyshell.pseudooutputfile object at 0x00000000027af358>
    8. >>>

If reload (SYS) is executed manually, these three variables of thesys module will be reset, causing the output to not be displayed in idle. So the solution is very simple, just need to copy these three variables before reload ,reload and then restore back to the line:

[Python]View PlainCopy
    1. >>> Stdi,stdo,stde=sys.stdin,sys.stdout,sys.stderr
    2. >>> Reload (SYS)
    3. >>> sys.stdin,sys.stdout,sys.stderr=stdi,stdo,stde
    4. >>> Print Sys.stdout
    5. <idlelib. Pyshell.pseudooutputfile object at 0x00000000027af320>
    6. >>>

A careful person might think that afterReload (SYS) , if the standard input, standard output, and standard error output do not work, then the sentence behind relaod (SYS) sys.stdin,sys.stdout, How can sys.stderr=stdi,stdo,stde execute it? Therefore, after reload (SYS) , before the recovery, the standard input can still work properly, can be checked by the following code:

[Python]View PlainCopy
  1. >>> Reload (SYS)
  2. >>> Sys.stdout=stdo
  3. >>> Print Sys.stdin
  4. <open file ' <stdin> ', mode ' R ' at 0x0000000001cab030>
  5. >>> Print Sys.stdout
  6. <idlelib. Pyshell.pseudooutputfile object at 0x00000000027af320>
  7. >>> Print Sys.stderr
  8. <open file ' <stderr> ', Mode ' W ' at 0x0000000001cab150>
  9. >>>

So the problem is solved, but it has to be stressed:

Never use reload (SYS) easily unless you completely erase the results! It is unwise to execute reload (SYS) just to be able to re-set Python's default encoding!

The Python designer intentionally deletes the setdefaultencoding () method inside the sys module After the python initialization is complete, in order not to change the Python default encoding at run time. To avoid some unknown problems. In fact, there are other, more reliable ways to solve the problem of coding.

Reasons why a command cannot be executed properly after the Python IDLE reload (SYS)

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.