Re: What is the Difference Between quit() and exit() commands in Python?

2019-09-16 Thread Peter Otten
Hongyi Zhao wrote:

> What is the Difference Between quit() and exit() commands in Python?

They are instances of the same type

>>> import inspect
>>> type(quit) is type(exit)
True
>>> print(inspect.getsource(type(quit)))
class Quitter(object):
def __init__(self, name, eof):
self.name = name
self.eof = eof
def __repr__(self):
return 'Use %s() or %s to exit' % (self.name, self.eof)
def __call__(self, code=None):
# Shells like IDLE catch the SystemExit, but listen when their
# stdin wrapper is closed.
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)


There is no difference, except for the name attribute and the repr() text:

>>> exit.name, exit.eof, exit
('exit', 'Ctrl-D (i.e. EOF)', Use exit() or Ctrl-D (i.e. EOF) to exit)
>>> quit.name, quit.eof, quit
('quit', 'Ctrl-D (i.e. EOF)', Use quit() or Ctrl-D (i.e. EOF) to exit)


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the Difference Between quit() and exit() commands in Python?

2019-09-16 Thread Eryk Sun
On 9/16/19, Hongyi Zhao  wrote:
>
> What is the Difference Between quit() and exit() commands in Python?

They're different instances of the Quitter class, which is available
if site.py is imported (i.e. not with the -S command-line option).
They're created by site.setquit():

def setquit():
"""Define new builtins 'quit' and 'exit'.

These are objects which make the interpreter exit when called.
The repr of each object contains a hint at how it works.

"""
if os.sep == '\\':
eof = 'Ctrl-Z plus Return'
else:
eof = 'Ctrl-D (i.e. EOF)'

builtins.quit = _sitebuiltins.Quitter('quit', eof)
builtins.exit = _sitebuiltins.Quitter('exit', eof)

exit(code) or quit(code) closes sys.stdin and raises SystemExit(code):

>>> quit.__class__


>>> print(inspect.getsource(type(quit)))
class Quitter(object):
def __init__(self, name, eof):
self.name = name
self.eof = eof
def __repr__(self):
return 'Use %s() or %s to exit' % (self.name, self.eof)
def __call__(self, code=None):
# Shells like IDLE catch the SystemExit, but listen when their
# stdin wrapper is closed.
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)

For example:

>>> try:
... quit(42)
... except BaseException as e:
... print(repr(e))
...
SystemExit(42,)
>>> sys.stdin.closed
True

Alternatively, sys.exit is a builtin function that's equivalent to
`raise SystemExit(code)` but does not close sys.stdin. For example:

>>> try:
... sys.exit(42)
... except BaseException as e:
... print(repr(e))
...
SystemExit(42,)
>>> sys.stdin.closed
False
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: What is the Difference Between quit() and exit() commands in Python?

2019-09-16 Thread wesley

  Hi

exit (http://docs.python.org/2/library/constants.html#exit"; rel="noreferrer) is 
an alias for quit (or vice-versa). They exist together simply to make Python more 
user-friendly.

please refer:  
https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used
 
(https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used)



-ursprüngliche Nachricht-
Von:  hongyi.z...@gmail.com (mailto:hongyi.z...@gmail.com)
Gesendet: 16.09.2019 14:35 Uhr
An:  python-list@python.org (mailto:python-list@python.org)
Betreff: What is the Difference Between quit() and exit() commands in Python?



What is the Difference Between quit() and exit() commands in Python?
--
 mail.python.org/mailman/listinfo/python-list 
(https://mail.python.org/mailman/listinfo/python-list"; target="_blank" 
rel="noopener)


-ursprüngliche Nachricht Ende-



--
https://mail.python.org/mailman/listinfo/python-list