Hacker News new | ask | show | jobs
by potatochup 1461 days ago
Hmm. Now you've got me thinking about the most ergonomic way do define a block in python. Maybe a no-op context manager? So you could do "with scope():"

I admit to using block scopes in rust a fair bit

2 comments

Ignoring any future/proposed syntax, I think the most block-like would be a local function definition which you then call once right afterward. This is the same you would do in any LISP-like language.

It could take zero arguments if you just want to temporarily extend the current scope with a few more variable-binding statements. Or, it could also have arguments if you want to do a "let" like construct to rename some expression results in the outer (calling) scope during the invocation.

  # ... outer scope
  x = expr1
  y = expr2
  r1 = None
  r2 = None
  
  def block1():
    nonlocal r1
    z = expr3
    r1 = expr4
  block1()
  
  def block2(x, y):
    nonlocal r2
    z = expr5
    r2 = expr6
  block2(expr7, expr8)
Edited for typos, code formatting, and bugs ;-).
Plain context manager doesn't actually introduce a new scope:

    with open("foo") as f:
        s = f.read()
    print(f) # closed file object still exists
A macro that looks like a context manager would work but beware the edge cases.