If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1.
Currently, I use `sys.exit([arg])` for exiting program and it works fine.
As described in the document:
If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1.
However, if I want to exit the program with status 0 (or any numbers else except 1) and print necessary messages before exiting, I have to write:
```python
print("message")
sys.exit()
```
So why `sys.exit` takes a list of arguments (`[arg]`) as its parameter? Rather than something like `sys.exit(code:int=0, msg:str=None)`?
I totally understand your reasoning here, but in some way it follows the unix philosophy: Do only one thing, but do that good.
That kind of thing is an open ended can of worms. You're better off
writing your own `aort()` function
BTW, `sys.exit()` actually raises a `SystemExit` exception which is
handled by the `sys.excepthook` callback which handles any exception
which escapes from the programme uncaught.
On 3/13/2023 10:34 PM, scruel tao wrote:
Lars:
I totally understand your reasoning here, but in some way it
follows the unix philosophy: Do only one thing, but do that good.
I understand, python is not strongly typed, so `sys.exit` will be
able to accept any types parameters rather than just integer. In
order to handle such “other” types logic, I think this function
already violated the unix philosophy, and there is no way to avoid
it.
Most Python folks will say the Python *is* fairly strongly typed, but
for a different definition of "type". That is, duck typing.
Lars:
I totally understand your reasoning here, but in some way it
follows the unix philosophy: Do only one thing, but do that good.
I understand, python is not strongly typed, so `sys.exit` will be
able to accept any types parameters rather than just integer. In
order to handle such “other” types logic, I think this function
already violated the unix philosophy, and there is no way to avoid
it.
Interesting, `raise SystemExit` seems to have the same behavior as `sys.exit`:
```shell
python -c "raise SystemExit(100)"
echo $?
<<< 100
python -c " import sys; sys.exit(100)"
echo $?
<<< 100
On 2023-03-14 03:29, Thomas Passin wrote:
On 3/13/2023 10:34 PM, scruel tao wrote:It's strongly typed but not statically typed, unless you count type
Lars:
I totally understand your reasoning here, but in some way it
follows the unix philosophy: Do only one thing, but do that good.
I understand, python is not strongly typed, so `sys.exit` will be
able to accept any types parameters rather than just integer. In
order to handle such “other” types logic, I think this function
already violated the unix philosophy, and there is no way to avoid
it.
Most Python folks will say the Python *is* fairly strongly typed, but
for a different definition of "type". That is, duck typing.
hints, which are optional.
On 3/13/2023 10:34 PM, scruel tao wrote:
Interesting, `raise SystemExit` seems to have the same behavior as `sys.exit`:
```shell
python -c "raise SystemExit(100)"
echo $?
<<< 100
python -c " import sys; sys.exit(100)"
echo $?
<<< 100
OTOH, you don't want to get too tricky:
(on Windows, obviously)
C:\Users\tom>py -c "import sys; sys.exit(type(100) == type('a'))"
C:\Users\tom>echo %ERRORLEVEL%
0
Presumably we wouldn't want to get an exit value of 0 for this case!
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (3 / 13) |
Uptime: | 30:10:21 |
Calls: | 10,391 |
Calls today: | 2 |
Files: | 14,064 |
Messages: | 6,417,093 |