sys.exit() [英] sys.exit()

查看:79
本文介绍了sys.exit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在如下代码中:


if len(sys.argv)< 2:

打印我需要参数!

sys.exit(1)


是sys.exit( )真的是个不错的选择?有更优雅的东西吗? (我

尝试返回,但它只在一个函数中有效)

-

-

每个足够先进的魔法与技术难以区分

- Arthur C Anticlarke

In a code such as:

if len(sys.argv) < 2:
print "I need arguments!"
sys.exit(1)

Is sys.exit() really a good choice? Is there something more elegant? (I
tried return but it is valid only in a function)
--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke

推荐答案

" Ivan Voras" < 4 **** @ fer.hr>在新闻中写道:bm ********** @ bagan.srce.hr:
"Ivan Voras" <iv****@fer.hr> wrote in news:bm**********@bagan.srce.hr:
在代码如下:

如果len (sys.argv)< 2:
打印我需要参数!
sys.exit(1)

sys.exit()真的是个不错的选择吗?有更优雅的东西吗? (我试过返回,但它只在一个函数中有效)
In a code such as:

if len(sys.argv) < 2:
print "I need arguments!"
sys.exit(1)

Is sys.exit() really a good choice? Is there something more elegant? (I
tried return but it is valid only in a function)




更优雅的可能是将你的代码放在一个可以让你的函数中/>
使用return,尽管如此你需要在某处调用sys.exit

想要设置返回代码。请记住,sys.exit只会抛出异常,

所以如果需要,你可以随时进一步捕获它。你也可以将打印结合到sys.exit的调用中并保存一行:


import sys


def main(args):

if len(args)< 2:

sys.exit(我需要参数!)


打印程序的其余部分......


if __name __ ==''__ main__'':

main(sys.argv)


-

Duncan Booth du****@rcp.co.uk

int month(char * p){return(124864 /((p [0] + p [1] -p [2]& 0x1f)+1)%12)[" \\\\\\\\

" \\\\ xb \\\\ x9 \ xa \\\\\\ 4"];} //谁说我的代码模糊不清?



More elegant might be to put your code in a function which would let you
use return, although even then you need to call sys.exit somewhere if you
want to set a return code. Remember that sys.exit just throws an exception,
so you can always catch it further out if you need to. Also you can combine
the print into the call to sys.exit and save a line:

import sys

def main(args):
if len(args) < 2:
sys.exit("I need arguments!")

print "rest of program..."

if __name__==''__main__'':
main(sys.argv)

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?


Ivan Voras写道:
Ivan Voras wrote:

代码如下:

如果len(sys。 argv)< 2:
打印我需要参数!
sys.exit(1)

sys.exit()真的是个不错的选择吗?有更优雅的东西吗? (我试过返回,但它仅在函数中有效)

In a code such as:

if len(sys.argv) < 2:
print "I need arguments!"
sys.exit(1)

Is sys.exit() really a good choice? Is there something more elegant? (I
tried return but it is valid only in a function)



sys.exit()是正确的,定义的,跨平台的退出方式来自

a程序并将值返回给调用程序。更改优雅的

定义,您可以轻松地将其视为所有解决方案中最优雅的b $ b。 ;-)


-Peter



sys.exit() is the proper, defined, cross-platform way to exit from
a program and return a value to the calling program. Change your
definition of elegant and you could consider it easily the most elegant
of all solutions. ;-)

-Peter


Peter Hansen写道:
Peter Hansen wrote:
Ivan Voras写道:
Ivan Voras wrote:
sys.exit()真的是一个不错的选择吗?有没有更优雅的东西?
(我试过返回但它只在函数中有效)
Is sys.exit() really a good choice? Is there something more elegant?
(I tried return but it is valid only in a function)


sys.exit()是正确的,定义的,跨平台的退出方式<一个程序并将值返回给调用程序。改变您对优雅的定义,您可以轻松地将其视为所有解决方案中最优雅的。 ; - )

sys.exit() is the proper, defined, cross-platform way to exit from
a program and return a value to the calling program. Change your
definition of elegant and you could consider it easily the most
elegant of all solutions. ;-)




好​​的。 :)


(仅供记录:我正在寻找一些不需要

模块导入的东西。但这并不重要。)


-

-

每一个足够先进的魔法都与技术难以区分

- 亚瑟C Anticlarke



Ok. :)

(Just for the record: I was looking for something that doesn''t require a
module import. But it is not important.)

--
--
Every sufficiently advanced magic is indistinguishable from technology
- Arthur C Anticlarke


这篇关于sys.exit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆