TJ1
TJ1

Reputation: 8498

Python 3: Local variable 'xyz' value is not used warning in PyCharm

Here is my code snippet:

def my_function(x, y):
    ...
    xyz = 0
    try:
       do_something
       xyz = 1
    except (SomeException) as e:
       do_somethingelse
       if condition A happens:
           xyz = 2
       else:
           xyz = 0

    if xyz == 0:
       print("Case 1 happened")
    elif xyz == 1:
       print("Case 2 happened")
    else:
       print("Case 3 happened")

    return xyz

So although I am using xyz and even use it as return argument but I get a warning message in PyCharm that says: Local variable 'xyz' value is not used. What is the reason, and how can I resolve this warning?

Upvotes: 0

Views: 800

Answers (1)

Javier Luna Molina
Javier Luna Molina

Reputation: 72

The first xyz = 0 can be omitted, because in the next block of code, you are redefining its value.

It will either go all as expected and xyz will be set to 1:

try:
       do_something
       xyz = 1

or it will catch the exception and set it to either 2 or 0:

except (SomeException) as e:
    do_somethingelse
    if condition A happens:
       xyz = 2
    else:
       xyz = 0

Upvotes: 1

Related Questions