Skip to content

Functions defined inside if blocks don't do variable lookup correctly #263

@LeszekSwirski

Description

@LeszekSwirski

If I define an inner function inside an if block, it fails to look up values that it closes over, doing e.g. a global lookup instead:

value = 4
def make_func(value):
    if value == 0:
        def inner():
            log.info(f"value = {value}")
        return inner

make_func(0)()
---
value = 4

Interestingly, however, if there is an inner function defined outside the if block, the one inside the if block works as it should.

value = 4
def make_func(value):
    if value == 0:
        def inner():
            log.info(f"value = {value}")
        return inner
    
    def fix():
        pass

make_func(0)()
---
value = 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy