0

I have a python boto3 code as below:

for rule in event_rules:
        if rule.startswith('Something'):
            print('[INFO] Proceeding!...')
            describe_rule = self.cw_event.describe_rule(
                Name=rule
            )
            if describe_rule['State'] == 'ENABLED':
                state = True
                break
        else:
            state = False
    return state
    .
    .
    .
if check == True:
       print('[INFO] SUCCESS' )          

else:
       print('[INFO] FAILED')

Here check is the class function which calls the above block from its function.I am pretty confident on the function and class part. In this case when a rule is present and is in ENABLED state,code is successfull. If the conditions are not matching, it throws out an error as follows:

UnboundLocalError: local variable 'state' referenced before assignment

I am not able to figure it out. I went through this answer : UnBoundLocalError: Local Variable Referenced Before Assignment [Counter] and gave state as global, but still failed.

Ebin Davis
  • 5,421
  • 3
  • 15
  • 20

1 Answers1

0

As mentioned in the comments, you should probably specify where the exception gets thrown, but at first look, if you get into if rule.startswith('Something'): but don't hit the second if statement [State] == 'ENABLED' then state won't be initiated and might throw the error on the return statement.

Bernhard
  • 1,253
  • 8
  • 18