[Python-ideas] Jump to function as an an alternative to call function
Stephen J. Turnbull
turnbull.stephen.fw at u.tsukuba.ac.jp
Fri Aug 17 02:47:11 EDT 2018
Jacob Solinsky writes:
> I think the way a "jump to" function rather than a "call function" would be
> implemented would be by removing the prologue and epilogue of the
> function's compiled code. Something vaguely like this:
First, let me apologize for pointing you at Ruby blocks. They don't
quite do what I thought they did; they're more like Python's
"nonlocal" than Lisp's "dynamic scope". I'm pretty sure you would
need something else added to Python. (But see below.) If you're not
a Ruby programmer and don't necessarily want to be one, I'd say wait
for someone who is to say I'm wrong before pushing them.
Second, it seems to me that the kind of "DRY" you're looking for is
just as well implemented by a macro facility. You might want to look
at MacroPy.
tl;dr here
I'm not very experienced with Ruby, so I'll post the snippets that
convinced me that Ruby's blocks don't DTRT. Maybe somebody else can
correct my code to get the job done, in which case you might have a
reason to advocate blocks for Python.
Steve
Code:
The problem is that blocks have nonlocal access (as defined in Python
3) to the scope in which they are defined, not somehow imposing
dynamic access on that scope.
def preamble
# assign local variables
end
def mutate
preamble do
# do stuff with preamble's locals
end
end
doesn't work because the block can see mutate's locals but not
preamble's.
So I think
def mutate
def worker
yield
# do stuff with mutate's locals
end
worker do
# assign local variables
end
end
works (untested), but it's just a complicated way to write
def mutate
# assign local variables
# do stuff with mutate's locals
end
while
def worker
yield
# do stuff with mutate's locals
end
def mutate
worker do
# assign local variables
end
end
does not work: worker can't access the local variables of mutate
because they're a scope it can't see.
--
Associate Professor Division of Policy and Planning Science
http://turnbull.sk.tsukuba.ac.jp/ Faculty of Systems and Information
Email: turnbull at sk.tsukuba.ac.jp University of Tsukuba
Tel: 029-853-5175 Tennodai 1-1-1, Tsukuba 305-8573 JAPAN
More information about the Python-ideas
mailing list