sys.exit()

Gerrit Holl gerrit at nl.linux.org
Thu Oct 9 10:14:03 EDT 2003


Ivan Voras wrote:
> 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)

An alternative that I often choose is:

raise SystemExit("I need arguments!")

This is the same in one line, and I think it is more elegant, because it
is higher-level: you are not using the low-level interface of error codes,
a non-programmer may not understand what '1' means, usually it means success
so that can be very confusing. I prefer raise SystemExit.

Gerrit.

-- 
30. If a chieftain or a man leave his house, garden, and field and
hires it out, and some one else takes possession of his house, garden, and
field and uses it for three years: if the first owner return and claims
his house, garden, and field, it shall not be given to him, but he who has
taken possession of it and used it shall continue to use it.
        -- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
	http://www.sp.nl/





More information about the Python-list mailing list