Unstack
Unstack

Reputation: 561

In Python's try... else... clause, why is the else claused parsed if try fails?

I'm coming across an issue with try... else... I'm testing that a variable has been set using try. If it has not been set, I just want to continue with the loop. If the variable has been set, I want to run the else part. However, Python throws a wobbly because it's trying to do the operations in the else part and failing because the variable has not been set. A bit of a Catch-22? Is there an alternative solution?

Code:

test = None
for num, line in enumerate(dataFile, 0):
    if myString in line:
        test = num
    try:
        test
    except:
        pass
    else:
        if (num - test) <= 58:
            ... do something ...

Upvotes: 0

Views: 101

Answers (3)

Juca
Juca

Reputation: 489

First, in your code you will not have an exception because test variable was created. As you never have a exception, the else clause always will be executed (this is what means the else in a try/except clause: run this part of the code if no exception was raised here).

If you just wanna know if a variable was setted and if it was not just continue the loop, you could do something like this:

# ...
for num, line in enumerate(dataFile, 0):
    # ...
    try: 
        test
    except NameError:
        # here you say 'skip the rest of loop in case of test was not setted'
        continue
   # here the rest of the code

In your case, maybe a easier way of doing the same is:

for num, line in enumerate(dataFile, 0):
    if myString in line:
        # in your code, if myString in line, test = num. So, num - test will allways be < 58 when myString in line

        # do something

Upvotes: 1

MrAlexBailey
MrAlexBailey

Reputation: 5289

Walking through your code... I'll simplify it a little bit to this:

foo = None

if foo:
    print 'Foo is not None'   # We never run this
try:
    foo    # This doesn't do anything, so this segment of the try / except will always end here
except:
    print 'The try segment did not raise an error'   # We also never get here
else:
    print 'Foo is none'   # We end up here because Foo is none, so this will print

Essentially... your try / except clause has no relevance to the if / then statement. This is because of your indentation.

So in your example if mystring not in line then everything in the else statement will execute.

You can much more easily check for a variable not being set like this:

if not foo:
    # Do something with foo if it doesn't exist
else:
    # Continue running your loop since foo was set

Upvotes: 2

Zach Gates
Zach Gates

Reputation: 4130

Try using an if statement to check if test exists as something other than a NoneType.

test = None
for num, line in enumerate(dataFile, 0):
    if myString in line:
        test = num
    if test is not None:
        if (num - test) <= 58:
            # do something

Or just getting rid of a second if statement entirely.

for num, line in enumerate(dataFile, 0):
    if (myString in line) and ((num - test) <= 58):
        # do something

Upvotes: 1

Related Questions