> I am truly, truly horrified that this a language option in perl, although I'm not surprised.
How about this in C(and thousand others): #define if while
I am pretty sure I can find something to horrify you in almost any language.
How about this in C(and thousand others): #define if while
I am pretty sure I can find something to horrify you in almost any language.
>>> import sys
>>> import ctypes
>>> pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
>>> five = ctypes.cast(id(5), pyint_p)
>>> 2 + 2 == 5
False
>>> five.contents[five.contents[:].index(5)] = 4
>>> 2 + 2 == 5
TrueHere's a small snippet that let you add methods to built-ins:
def inject(cls, wrapper=lambda x: x, name=None):
def _builtin_hack(name):
import ctypes as c
_get_dict = c.pythonapi._PyObject_GetDictPtr
_get_dict.restype = c.POINTER(c.py_object)
_get_dict.argtypes = [c.py_object]
return _get_dict(name).contents.value
def wrap(method):
name_ = name or method.func_name
method = wrapper(method)
try:
setattr(cls, name_, method)
except:
_builtin_hack(cls)[name_] = method
return wrap
@inject(dict)
def extend(self, new_dict):
return dict(self, **new_dict)
print {'a':2}.extend({'b':3})I think I discovered the hack when I found out about the id() function, and hex(id(5)) looked suspiciously like a pointer value. (And http://docs.python.org/library/functions.html#id confirms.) http://docs.python.org/library/ctypes.html has usually been pretty useful. Apart from that, just play.
$ python -c "import ctypes; ctypes.memset(0, 0, 42)"
Segmentation fault #define break continue
Or Ruby: irb(main):001:0> if 0
irb(main):002:1> print "True!"
irb(main):003:1> end
True!=> nil
(Though the Ruby example probably not as horrific as the others)