Skip to content

Variable capture does not workout without encapsulation #148

@mebenstein

Description

@mebenstein

In the following code, variables are not captured if I write code directly inside the Parla context. Not sure if this is intended behavior, if so it is not documented anywhere and certainly should be mentioned in the first tutorial.

def fun():
    fun = TaskSpace('fun')
    for i in range(4):
        @spawn(fun[i])
        def print_fun():
            print(f'Fun {i}', flush=True)

if __name__ == "__main__":
    print("start")
    with Parla():
        no_fun = TaskSpace('no_fun')
        for i in range(4):
            @spawn(no_fun[i])
            def print_no_fun():
                print(f'No fun {i}', flush=True)

    with Parla():
        fun()

This program outputs:

No fun 3
No fun 3
No fun 3
No fun 3
Fun 0
Fun 2
Fun 1
Fun 3

Using nonlocal in the first part also leads to a syntax error.

Metadata

Metadata

Labels

TutorialAnything pertaining to improving the Parla tutorial.wontfixThis will not be worked on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions