2

I am not entirely sure whether or not this behavior is to be expected or not, but it is definitely quite strange. When you have some code like this:

def a_function():
    if a_list != some_other_list:
        print(a_list)

It works fine and I had no issue with it. However, if you change it to:

def a_function():
    if a_list != some_other_list:
        a_list = some_other_list

Suddenly, a problem occurs where it says that a_list on line 2 is an unresolved reference. Why should what is in the if statement affect whether a_list can be resolved? Is this kind of thing normal? Is it perhaps a bug in Python 3.6.1 or PyCharm (Community Edition 2017.1.5)? Any help on clarifying this would be greatly appreciated.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Ingsoc
  • 23
  • 1
  • 4
  • 3
    Comparison is done with ==, not with =. – Jacques de Hooge Aug 29 '17 at 19:36
  • 1
    Can you post the full code (definition of `a_list` and `some_other_list`)? Your 2nd line (modified) would generate a _SyntaxError_, but according to you it doesn't (so the `=` might be a typo?). – CristiFati Aug 29 '17 at 19:41
  • Line `3` is the replacing the `print()`. When you assign to `a_list` it is trying to create a local `a_list`, which means your use of `a_list` in the comparison now fails. Doing `global a_list` on the first line of the function would fix this but you really should look into passing and returning values into the function vs. global variables. – AChampion Aug 29 '17 at 19:43

3 Answers3

5

As long as you just access variables that are not local to your function everything works fine as long as they are found somewhere in the outer scopes. But as soon as you assign to a variable it becomes local to your function. Python won't look in the outer scopes for the variable name anymore. So when you include:

a_list = some_other_list

The line:

a_list != some_other_list

will fail because it doesn't look for a_list outside your function anymore and if you define it later in the function it's an UnboundLocalError or similar.

I did go into more details in another answer if you're interested.


In case you just wanted to compare for equality change = (assignment) to == (equality check).

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • So python first differentiates the local variables based on the assignments in the fun before executing the function. even if the assignment is within if condition? – akp Aug 29 '17 at 19:54
  • 1
    yes, even if it would be in an unreachable `if False:` block inside the function :) Of course arguments of the function are also local :) – MSeifert Aug 29 '17 at 19:56
1

edit: in python, if you assign a variable inside the scope of the function then it becomes a local variable. so a_list becomes local when you a_list = some_other_list and Python tells you by throwing the Unresolved Reference Error.


also, the if statement expects an expression that returns a boolean value.

a_list = some_other_list simply set some_other_list to refrence the same memory address as some_other_listequal which does notreturn a boolean.

However a_list == some_other_list performace a comparison that returns a boolean of True or False to indicate the value equality of two variables.

OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
0

There is a big difference between = and == in python.

a = b is assigning the value of b to the variable a.

a == b is comparing the value of a to the value of b.

In your case you want to use == which compares the two values and returns True if they are the same value, otherwise it will return False.

AllenMoh
  • 476
  • 2
  • 13