Git Product home page Git Product logo

Comments (4)

mbyrnepr2 avatar mbyrnepr2 commented on July 4, 2024 1

Hi @LLyaudet :)

My understanding is that in the first example the assignment y -= 1 is the problematic part: this assignment makes y refer to something that is local to the test_closure_result function and stops referring to the parameter of test_closure_provider.

"""example1.py"""
def test_closure_provider(y):
    """A doc string"""
    def test_closure_result(z):
        """Another doc string"""
        while y > 0:
            z *= 2
            y -= 1
        return z
    return test_closure_result

one = test_closure_provider(1) 
print(one(1))

python example1.py:

  File "/Users/markbyrne/programming/y.py", line 7, in test_closure_result
    while y > 0:
          ^
UnboundLocalError: cannot access local variable 'y' where it is not associated with a value

The above issue is not realised in the second example because the inner function is simply reading y:

"""example2.py"""

def test_closure_provider(y):
    """A doc string"""

    def test_closure_result(z):
        """Another doc string"""
        return z + y

    return test_closure_result

one = test_closure_provider(1)
print(one(1))

python example2.py:
2

SO post similar issue: https://stackoverflow.com/questions/29639780/unboundlocalerror-local-variable-referenced-before-assignment-in-python-closure

from pylint.

LLyaudet avatar LLyaudet commented on July 4, 2024 1

Thanks @mbyrnepr2 :)
I'm not a specialist of nonlocal keyword.
Now I understand that it is sometimes needed and sometimes not,
because Python has a "best effort" approach until both behaviours may be possible and then the use of nonlocal becomes significant :)

from pylint.

LLyaudet avatar LLyaudet commented on July 4, 2024

Hello :)
I really tried to get a minimal example, inner function is required and while loop also.
I hope it helps.
Best regards,
Laurent Lyaudet

from pylint.

LLyaudet avatar LLyaudet commented on July 4, 2024

My bad, I thought that Python dealed with closures like JS. I added nonlocal keyword and the error is gone.
But I had other arguments without the error so it is not a False positive it is a False negative:

I should have the error and the warning with the following code without the while loop and without nonlocal.
And I have None.

"""A module doctring"""

def test_closure_provider(y):
    """A doc string"""

    def test_closure_result(z):
        """Another doc string"""
        return z + y

    return test_closure_result

from pylint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.