Rebinding 0 (was Re: "?:", "a and b or c" or "iif")
Tim Peters
tim_one at email.msn.com
Thu May 27 22:46:12 EDT 1999
[Tim]
> If the revolting "(x and [a] ..." got popular, a peephole optimizer
> could get rid of the list ops -- but only at the cost of frustrating
> Michael and Neel's attempts to change what the generated code maps
> "0" to <0.9 wink>.
[Neel Krishnaswami]
> How *would* you rebind 0 without hacking the parser?
The answer is 42, right?
def f():
print 0 + 0
c = f.func_code
c2 = []
for attr in ('co_argcount',
'co_nlocals',
'co_stacksize',
'co_flags',
'co_code',
'co_consts',
'co_names',
'co_varnames',
'co_filename',
'co_name',
'co_firstlineno',
'co_lnotab'):
c2.append(getattr(c, attr))
c2[5] = (None, 21)
import new
f.func_code = apply(new.code, tuple(c2))
f() # guess what this prints
Download Michael's bytecode hacks and this kind of fiddling is much easier.
> ...
> Now, I'm not asking this simply to prove my bad character, but because
> I am thinking that this might make it simpler to implement a macro
> system for Python (in Python, preferably). By that I don't mean a
> simple text preprocessor like C's, but an honest-to-god hygienic macro
> facility, because I could create symbols and manipulate symbols that
> aren't legal Python identifiers.
Michael has gone a bit in that direction already. It's reprehensible, but I
encourage it <wink>.
> (And then all I need are continuations, and I'll be happy. Honest.)
Hmm. Guido has all but promised he'd get a sex-change operation before he'd
add user-visible continuations to Python -- and I suspect most Python users
would find the former change less confusing.
if-python-were-scheme-there'd-be-no-need-for-either-ly y'rs - tim
More information about the Python-list
mailing list