|
|
|
|
|
by stryker
5207 days ago
|
|
I too am more interested in the bytecode hackery that's going on here than the fact that it is a goto implementation. You might be interested in byteplay (http://code.google.com/p/byteplay/), which is a module that tries to simplify this kind of process. Two points:
1) Do not take the presented code as evidence that arbitrarily new syntax can be created. Any code that raises a SyntaxError will still be invalid. 2) I believe there is a legitimate reason to use bytecode hackery -- I've come across it many times: the slowness of Python. For example, let's say I have three functions as follows. def f(a):
return a + 1 def g(a):
return 2a def h(a):
return 2a + 1 It's clear that f(g(x)) == h(x) for all values of x. But h will run faster (marginally in this case, but try to imagine more complicated cases) because the Python interpreter will never make such an optimization. Function calls will be made, frames generated, etc. -- and that's where bytecode hackery comes in! Imagine pasting f and g together so that I never have to write h but still get the benefit of speed. I do recognize that in many cases, it may be preferable to just use a more low-level language to optimize away such functions, but I always like to envision a pure Python solution first. |
|