Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 299 – Special __main__() function in modules

Author:
Jeff Epler <jepler at unpythonic.net>
Status:
Rejected
Type:
Standards Track
Created:
12-Aug-2002
Python-Version:
2.3
Post-History:
29-Mar-2006

Table of Contents

Abstract

Many Python modules are also intended to be callable as standalonescripts. This PEP proposes that a special function called__main__()should serve this purpose.

Motivation

There should be one simple and universal idiom for invoking a moduleas a standalone script.

The semi-standard idiom:

if__name__=='__main__':perform"standalone"functionality

is unclear to programmers of languages like C and C++. It also doesnot permit invocation of the standalone function when the module isimported. The variant:

if__name__=='__main__':main_function()

is sometimes seen, but there exists no standard name for the function,and because arguments are taken from sys.argv it is not possible topass specific arguments without changing the argument list seen by allother modules. (Imagine a threaded Python program, with two threadswishing to invoke the standalone functionality of different moduleswith different argument lists)

Proposal

The standard name of the ‘main function’ should be__main__. When amodule is invoked on the command line, such as:

pythonmymodule.py

then the module behaves as though the following lines existed at theend of the module (except that the attribute __sys may not be used orassumed to exist elsewhere in the script):

ifglobals().has_key("__main__"):importsysas__sys__sys.exit(__main__(__sys.argv))

Other modules may execute:

importmymodulemymodule.__main__(['mymodule',...])

It is up tomymodule to document thread-safety issues or otherissues which might restrict use of__main__. (Other issues mightinclude use of mutually exclusive GUI modules, non-sharable resourceslike hardware devices, reassignment ofsys.stdin/stdout, etc)

Implementation

Inmodules/main.c, the block near line 385 (after thePyRun_AnyFileExFlags call) will be changed so that the above code(or its C equivalent) is executed.

Open Issues

  • Should the return value from__main__ be treated as the exit value?

    Yes. Many__main__ will naturally returnNone, whichsys.exit translates into a “success” return code. In those thatreturn a numeric result, it behaves just like the argument tosys.exit() or the return value from C’s main().

  • Should the argument list to__main__ includeargv[0], or just the“real” argumentsargv[1:]?

    argv[0] is included for symmetry withsys.argv and easytransition to the new standard idiom.

Rejection

In a short discussion on python-dev[1], two major backwardscompatibility problems were brought up and Guido pronounced that hedoesn’t like the idea anyway as it’s “not worth the change (in docs,user habits, etc.) and there’s nothing particularly broken.”

References

[1]
Georg Brandl, “What about PEP 299”,https://mail.python.org/pipermail/python-dev/2006-March/062951.html

Copyright

This document has been placed in the public domain.


Source:https://github.com/python/peps/blob/main/peps/pep-0299.rst

Last modified:2025-02-01 08:59:27 GMT


[8]ページ先頭

©2009-2025 Movatter.jp