mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[BUG] Block scoping works differently in Python and in Mojo

Open remiconnesson opened this issue 1 year ago • 5 comments

Bug Description

block scoping works differently in Pythonand in Mojo, the variable declared inside the loop is available in Python but not

  • expected behavior: valid python code should be valid mojo code
image

Steps to Reproduce

inside a mojo notebook,

%%python
for i in range(10):
    j = i
print(j)  # 9
# mojo 
for i in range(10):
    j = i
print(j)  # ERROR, use of unknown declaration 'j'

Context

%%python
import subprocess

subprocess.Popen(["cat", "/proc/self/cgroup"])

-->

11:cpu,cpuacct:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
10:cpuset:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
9:perf_event:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
8:blkio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
7:hugetlb:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
6:memory:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
5:net_cls,net_prio:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
4:devices:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
3:pids:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
2:freezer:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
1:name=systemd:/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-podee07931e_50d3_4b52_964b_2a8ec758f348.slice/cri-containerd-92f2e33f4363f0365bc3645c7bd817bfa98d53f0fae90447dfbf81f06ea86351.scope
0::/

remiconnesson avatar May 12 '23 07:05 remiconnesson

To be fair, it is not recommended to write code like that, as you might get the case where j is not defined, if the loop never enters (e.g. iterating over empty list). Instead, always initialize j = None or so before the loop.

albertz avatar May 12 '23 10:05 albertz

To be fair, it is not recommended to write code like that, as you might get the case where j is not defined, if the loop never enters (e.g. iterating over empty list). Instead, always initialize j = None or so before the loop.

Yup that's fair

I was actually playing around to see if let was blocked scoped (like in js) and stumbled upon this (and which I prefer tbh)

but that's not a question of preferences,

the intent of mojo is to become a superset of python so this might be something of intereset for the team behind mojo

remiconnesson avatar May 12 '23 16:05 remiconnesson

If Mojo catches this, it's actually a good thing for me.

ksandvik avatar May 12 '23 16:05 ksandvik

This looks like a notebook environment specific problem. This should work inside a def

Mogball avatar May 18 '23 03:05 Mogball

@Mogball This doesn't work inside of a def either right now, it's possible that the implicitly defined variable isn't being added to the right scope.

River707 avatar Jun 07 '23 23:06 River707