News for hackers. Stuff that matters.
Re:[Python-ideas] List Revolution by phzbOx on Saturday September 10, 2011 @09:03AM (#2981504)
May I ask how did you find about that? Sometime I would like to play in ctypes but I'm not sure where to start :-/

Here'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})
Re:[Python-ideas] List Revolution by Jach on Saturday September 10, 2011 @08:19PM (#2982867)
ctypes is like god-mode for Python... I like your hack too. I just play around with it from time to time, I believe the only "serious" thing I've done with it so far is making a Python interface around a C library I made.

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.