免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5243 | 回复: 8

exit(0),exit(1)中的exit()是什么,函数吗?? [复制链接]

论坛徽章:
0
发表于 2018-03-28 18:12 |显示全部楼层
本帖最后由 zhsx616 于 2018-03-28 18:20 编辑

比如代码:
print 'hello world'
exit(0)
运行时就会退出程序了通过百度得知exit(0)表示“无错误退出”,但是我还是搞不明白这里的exit()的“身份是什么”。它没有前缀(比如sys.exit),说明它不是某个模块的内容,那就说不定是内置函数,于是我去翻了一下python手册(我的是《python参考手册》第4版修订版),结果python手册中的所有内置函数中,e开头的内置函数就只有enumrate(),eval(),exec(),并没有exit()


那这个exit()到底是什么“身份”呢,函数?方法?还是类似于指令什么的?



论坛徽章:
0
发表于 2018-03-29 13:12 |显示全部楼层
https://www.cnblogs.com/weiman3389/p/6047062.html

这种问题多多百度。
python都是调用module。

论坛徽章:
0
发表于 2018-03-30 13:20 |显示全部楼层
回复 2# dahe_1984

我是百度之后不明白才来问的。你给的结果我也百度到了,就是没看懂


exit()/quit(), 抛出SystemExit异常. 一般在交互式shell中退出时使用


意思我懂,exit()用于抛出异常,可是我就是搞不清楚,它算不算函数,因为我在参考手册的内置函数中没有找到exit(),说明它不是内置函数,而它又没有类似于sys.exit()或者os.exit()的前缀,说明它不是某个模块的。既不是内置函数又不是某个模块的函数,那它算不算“指令”?比如cd e:\中的cd。exit()的性质是和cd一样的吗??


比如还有exit(0),exit(1),exit(-1),exit(-100)等等,如果exit()是函数,那么0,1,-1,-100都是它的参数,那这些参数是怎么用的??如果它不是函数,那这些0,1,-1,-100是什么?


我也不是没百度就瞎问,实在是百度也没找打结果才来求助的

论坛徽章:
0
发表于 2018-03-30 14:02 |显示全部楼层
hegc@DESKTOP-0E940NO /cygdrive/e/python/faceswap
$ python -c "import sys; sys.exit(1)"; echo $?
1

hegc@DESKTOP-0E940NO /cygdrive/e/python/faceswap
$ python -c "import sys; sys.exit(10)"; echo $?
10

hegc@DESKTOP-0E940NO /cygdrive/e/python/faceswap
$ python -c "import sys; sys.exit(255)"; echo $?
255

hegc@DESKTOP-0E940NO /cygdrive/e/python/faceswap
$ python -c "import sys; sys.exit(256)"; echo $?
0

hegc@DESKTOP-0E940NO /cygdrive/e/python/faceswap
$ python -c "import sys; sys.exit(-1)"; echo $?
255

hegc@DESKTOP-0E940NO /cygdrive/e/python/faceswap
$ python -c "import sys; sys.exit(-2)"; echo $?
254

论坛徽章:
0
发表于 2018-03-30 14:03 |显示全部楼层
我记得返回值是0~255之间,如果是负数,返回255+负数。

这个是函数,import sys

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2018-03-30 18:21 |显示全部楼层
回复 1# zhsx616

$ python -c "exit(2)"; echo $?
2

$ python -c "import sys;sys.exit(2)"; echo $?
2

$ python -c "print(type(exit))"
<class '_sitebuiltins.Quitter'>

$ python -c "import sys;print(type(sys.exit))"
<class 'builtin_function_or_method'>


论坛徽章:
0
发表于 2018-03-31 17:54 |显示全部楼层
回复 5# dahe_1984

可是我在没有导入sys模块的情况下,exit()也是可以运行的,这是为什么??

论坛徽章:
0
发表于 2018-03-31 17:55 |显示全部楼层
回复 6# jason680

没懂。。。这是某种测试吗??

论坛徽章:
0
发表于 2018-03-31 19:53 |显示全部楼层
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:5 [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit.__module__
'site'
>>>

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 = 'Cmd-Q'
    elif os.sep == '\\':
        eof = 'Ctrl-Z plus Return'
    else:
        eof = 'Ctrl-D (i.e. EOF)'

    class Quitter(object):
        def __init__(self, name):
            self.name = name
        def __repr__(self):
            return 'Use %s() or %s to exit' % (self.name, 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)
    __builtin__.quit = Quitter('quit')
    __builtin__.exit = Quitter('exit')

你要是这么学习一门语言,我觉得你可能不适合学习python。 学语言是为了解决问题,不是制造更多的问题。

可能不太中听。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP