[Python] pycharm erroneously marks seemingy undefined variable as 'error' (unresolved...

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    [​IMG]

    Pycharm (Pycharm 2022.3.3 Community Edition) shows an error (unresolved reference) in the code attached in the image. In the function "func_test1" the variable "a" seems to be ?shadowed? by the variable "a" of the object "obj". In the function "func_test2" there is no issue about the variable "anythingexcepta". Upon running the script, everything works just fine. The three prints are: 5, 12 and 22, just as expected (i am using python 3.9).

    So my questions are:

    1. Why does this error occur? Is it some kind of shadowing? (although the script runs just fine)
    2. Why does it occur in "func_test1", but nor in "func_test2"?
    3. Is the behaviour of pycharm intended behaviour, if yes, why?
    4. Is there a way to get rid of this erroneous error, apart from naming the variable something else.

    I made this test script to showcase the error, ran the script and it ran just fine. But i still have no idea, why the error shows in the first place.

    here's the code of the test script:

    class testClass:
    def __init__(self, a):
    self.a = a

    def func_test1(a):
    def func_inner1(obj):
    obj.a += a
    return func_inner1

    def func_test2(anythingexcepta):
    def func_inner2(obj):
    obj.a += anythingexcepta
    return func_inner2


    obj = testClass(5)

    func_1 = func_test1(7)
    func_2 = func_test2(10)

    print(obj.a)
    func_1(obj)
    print(obj.a)
    func_2(obj)
    print(obj.a)

    Continue reading...

Compartilhe esta Página