Replies: 6 comments 21 replies
-
This is not true, keeping the last variable in scope is standard python. Maybe keeping it in scope within just the cell makes sense- but maybe frustrating if you did want the correct behavior. But this is a common complaint. what about implicitly private variables being declared: # suggested as import because it's easiest to hook into the ast
from marimo.private import i, j, k, dynamic_name
# reuse i as much as you want across cellsAnother suggestion (harder to implement, and less elegant) from marimo import range, enumerate, zipWhere the imported behavior is what you proposed? (Users could also do Here are some other suggestions that have been mentioned before:
|
Beta Was this translation helpful? Give feedback.
-
|
I'm not sure if I like the dynamic imports. What about something like this? import marimo as mo
let i, j, k = mo.locals()
# use i, j, k wherever you like The AST can infer how many variables that |
Beta Was this translation helpful? Give feedback.
-
|
I still think that just allowing variables that are first written to (not read) in all cells where they appear would be nice and safe. #1477 |
Beta Was this translation helpful? Give feedback.
-
|
+1 from a lecture notebook author, with a concrete use case that may help frame the trade-off. I'm exploring using marimo to deliver postgraduate lectures with 60–100 cells per notebook, mostly plotting/demo cells that compute intermediate values just to render a figure. None of those intermediates need to escape the cell, and I'm bulk converting from exisitng Jupyter, so I am prefixing everything with _ (admittedly over-zealously). A typical cell looks like: app.cell(hide_code=True)
def _(np, plt, cp):
_dist = cp.Normal(0, 1)
_expansion = cp.generate_expansion(3, _dist, normed=True)
_x = np.linspace(-3, 3, 200)
_phi_x = _expansion(_x).T
_fig, (_ax1, _ax2) = plt.subplots(1, 2)
_ax1.plot(_x, _phi_x)
# ... 15 more lines, every variable prefixed ...
_fig
return@dmadisetti's point that a context manager doesn't add much over the "wrap in a function" autofix is fair for short cells. For the long demo cells I have, the function-wrapping pattern ( To me the declarative alternative ( The strongest argument I'd add for some form of explicit-locality marker (whatever the syntax) is that the friction isn't really about typing the underscores. It's about the cognitive load when reviewing or modifying a cell: in the example above, almost every variable name has a sigil that has to be visually filtered out. For lecture material that's read by students and reviewed by colleagues who aren't marimo-fluent, this is the biggest cost. If the team's preferred direction is the idiomatic-name allowlist, that would solve 90% of my use case with zero new syntax: fig, ax, axs, axes would just stop counting as global definitions. I'd happily contribute a default list of plotting-related names if that helps move things forward. |
Beta Was this translation helpful? Give feedback.
-
|
Perhaps it would suffice for Marimo to honor the @app.cell
def _():
x = 0
del x
return
@app.cell
def _():
x = 0
return (x,)
@app.cell
def _(x):
x
return |
Beta Was this translation helpful? Give feedback.
-
|
I have framed the |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
Suggestion: Scope for-loop iterator variables to their loop context
Problem
This pattern is one of the most common constructs in Python programming. Typically, the
itemvariable is intended to be temporary and only meaningful within the loop's context. However, in Python, this variable persists in the namespace after the loop completes.In marimo, this behavior creates unexpected issues when the same iterator variable name is reused across different cells:
Proposed Solution
I suggest that marimo treats for-loop iterator variables as scoped to their loop context by default. This would better align with the programmer's intent, where these variables are typically meant to be temporary.
Current Workarounds
Currently, users must:
_itemto indicate temporary statusBenefits
Implementation Considerations
This could be implemented as:
Would love to hear your thoughts on this suggestion!
Beta Was this translation helpful? Give feedback.
All reactions