You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Starlark and Python, within a function, an assignment statement creates a variable binding in the function, whether or not it is executed:
$ starlark
>>> def f():
... if False:
... x = 1
... print(x)
...
>>> f()
Traceback (most recent call last):
<stdin>:1:2: in <expr>
<stdin>:4:11: in f
Error: local variable x referenced before assignment
$ python3
>>> def f():
... if False:
... x = 1
... print(x)
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in f
UnboundLocalError: cannot access local variable 'x' where it is not associated with a value
>>>
But Starlark differs from Python in its treatment of the toplevel. The spec disallows loops and conditional assignments at toplevel (conditional assignments are just loops where n < 2!), and the -globalreassign flag in the Go implementation treats the toplevel like any other function.
Here's 2 scripts with -globalassign enabled, run the first one, and use its output as input/predeclared for the second one:
Script 1:
Script 2:
It runs fine, but if you change the second script to:
It will throw an unexpected error: global variable hello referenced before assignment
Why?
The text was updated successfully, but these errors were encountered: