It could also be done (though not as cleanly) by making macros act as import hooks.
import defmacro # Stop processing until defmacro is loaded. # All future lines will be preprocessed by the # hook collection ... from defmacro import foo # installs a foo hook, good for the rest of the file
Brrr. What about imports that aren't at the top level (e.g. inside a function)?
Why not just introduce macros?
Because I've been using Python for 15 years without needing them? Sorry, but "why not add feature X" is exactly what we're trying to AVOID here. You've got to come up with some really good use cases before we add new features. "I want macros" just doesn't cut it.
If the answer is "We should, it is just hard to code", then use a good syntax for macros. If the answer is "We don't want
xx sss (S\<! 2k3 ]
to ever be meaningful", then we need to figure out exactly what to prohibit. Lisp macros are (generally, excluding read macros) limited to taking and generating complete S-expressions. If that isn't enough to enforce readability, then limiting blocks to expressions (or even statements) probably isn't enough in python.
I suspect you've derailed here. Or perhaps you should use a better example; I don't understand what the point is of using an example like "xx sss (S<! 2k3 ]".
Do we want to limit the changing part (the "anonymous block") to only a single suite? That does work well with the "yield" syntax, but it seems like an arbitrary restriction unless *all* we want are resource wrappers.
Or loops, of course.
Pehaps you've missed some context here? Nobody seems to be able to come up with other use cases, that's why "yield" is so attractive.
Or do we really just want a way to say that a function should share its local namespace with it's caller or callee? In that case, maybe the answer is a "lexical" or "same_namespace" keyword. Or maybe just a recipe to make exec or eval do the right thing.
def myresource(rcname, callback, *args): rc=open(rcname) same_namespace callback(*args) close(rc)
def process(*args): ...
But should the same_namespace modifier be part of the call site or part of the callee? You seem to be tossing examples around a little easily here.