0

I have a question regarding the error local variable ... referenced before assignment in Python3. Several cases of this error have been discussed in this forum (look at here); however they don't address my question. My problem at the moment is not how to resolve this issue, but to understand why there is such an issue.

Here is my code:

class Test:
    def __init__(self,n):
        if n > 0:
            self.index = n
            self.check = v[0]
        else:
            self.index = n+5
            self.check = v[1]        
            v = [18,12]

v = [90,43]
g = Test(18)

The problem arises only because of that v=[18,12] assignment. If it is erased, there won't be any error. However, my issue is that 18 > 0, and as a result, the part of the code that comes after the else must be ignored. Thus, why is there such an error?

CLAUDE
  • 294
  • 1
  • 2
  • 11
  • Can you fix the formatting of your code in the question? Are the last 2 lines inside the class or not? – gen_Eric Dec 01 '20 at 17:59
  • @RocketHazmat No they are **not** inside class. I will edit it now. Thanks – CLAUDE Dec 01 '20 at 18:00
  • Also, what is `v = [90,43]` supposed to be? Have you tried to put that before the `__init__` and then referencing `self.v` instead? – gen_Eric Dec 01 '20 at 18:01
  • By putting ```v=[90,43]``` before ```__init__``` the problem is not resolved. However; as I said, at the moment I don't want to know how to resolve this issue, but want to know why there is an error at all. – CLAUDE Dec 01 '20 at 18:04
  • There is an error because `__init__` doesn't know what `v` is. That should probably be `self.v` instead. If you set `v = [90,43]` inside the class (before `__init__`) then it can access that list as `self.v`. If you don't want to do that, then you'll have to _pass_ `v` as a parameter to `__init__`. – gen_Eric Dec 01 '20 at 18:06
  • `v` has local scope because of the `v = [18,12]` within your `__init__` method. It doesn't matter that you're in a different branch of your `if`/`else` statement. Simply having the assignment within your method means any other references to it will be referring to the local variable. – Axe319 Dec 01 '20 at 18:08
  • 2
    @RocketHazmat: nah, `__init__` does know. That's how global vars work in python. – Sergio Tulentsev Dec 01 '20 at 18:08
  • @RocketHazmat If you erase ```v=[18,12]``` then there is no error. Therefore, eventhough ```v=[90,43]``` is defined outside the class, the class will recognize it. – CLAUDE Dec 01 '20 at 18:09
  • @SergioTulentsev I guess you're right, my bad. I've just never coded this way. If I were to do this, then `v` would be a parameter to `__init__` or it'd be referenced as `self.v`. – gen_Eric Dec 01 '20 at 18:11

1 Answers1

2

want to know why there is an error at all

The problem is that your assignment creates a local variable v, that shadows your global variable v. This happens at parse/load time. That's why python knows that you tried to use the local variable before giving it any value.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367