Python Forum
why I can't use "try" without "expect"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why I can't use "try" without "expect"
#1
Question 
why this rule is enforceable? it makes my code more nested and unpleasant to read

EnableLoop = 0

try:
    EnableLoop = bool(sys.argv[2])
except: 
    EnableLoop = 0
    #pass
    
Reply
#2
This code is strange because bool() will never fail, so assuming sys.argv has the usual meaning, the code can raise an exception only when there are less than two command line arguments. You could replace the code with
EnableLoop = (len(sys.argv) >= 3)
When handling command line arguments it is much easier to use a module such as argparse. By scanning sys.argv, you are reinventing the wheel.

You can't use try without except, but you could use contextlib.suppress()
with suppress(Exception):
    EnableLoop = bool(sys.argv[2])
xMaxrayx likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(May-16-2024, 04:56 PM)Gribouillis Wrote: This code is strange because bool() will never fail, so assuming sys.argv has the usual meaning, the code can raise an exception only when there are less than two command line arguments. You could replace the code with
EnableLoop = (len(sys.argv) >= 3)
When handling command line arguments it is much easier to use a module such as argparse. By scanning sys.argv, you are reinventing the wheel.

You can't use try without except, but you could use contextlib.suppress()
with suppress(Exception):
    EnableLoop = bool(sys.argv[2])


I see thanks yeah it give error when you don't put any argument,

Exception has occurred: IndexError
list index out of range
but yeah great way to use arguments.lenght instead seems it's great for bool but not for str or int.


I see thanks for suggestions i will learn argparse library and I didn't know about contextlib, thx for the help <3
Reply
#4
EnableLoop = 0 if len(sys.argv) < 3 else int(sys.argv[3])
some_string = "" in len(sys.argv < 4) else sys.argv[4]
Or better yet
EnableLoop = 0
some_string = ""
try:
    EnableLoop = int(sys.argv[3])
    some_string = sys.argv[4]
except:
    pass
I agree with Gribouillis. Except for really simple cases where you only need to grab one value from the command line, it is much better to use something like argparse. Even for one value I'd use argparse if there is any kind of error checking. And with argparse you get usage help for free.
Reply
#5
You should avoid a bare except, because it will catch all exceptions, which could lead into errors you won't able to find.

If you don't know, which exception when occurs, test it in the REPL.

import sys
sys.argv[666]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    sys.argv[666]
IndexError: list index out of range
So now, you know that an IndexError ocours. You should catch this explicit Exception. Btw. Exceptions are used much in Pythons control flow. e.G. the for-loop catches the StopIteration exception.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Not able to read the text using pexpect/expect Bipinjohnson 7 4,315 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Please help me. about self.expect() leeyoung 0 1,355 Nov-23-2020, 10:25 PM
Last Post: leeyoung
  can I use 2 child.expect at the same time? korenron 0 1,578 May-25-2020, 10:41 AM
Last Post: korenron
  Trying to convert my Expect/bash to Python sumncguy 4 4,214 Jun-07-2019, 07:14 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020