[Cython] Speedup module-level lookup

Chris Colbert sccolbert at gmail.com
Thu Jan 19 08:42:11 CET 2012


AFAIK, a module's dict is readonly, so I don't believe a dict subclass will
work there (I could be wrong) unless you hack up the module object from C.
You can do it with descriptors on a ModuleType however, which should be
plenty fast from Cython-land.

In [16]: class AGetter(object):
   ....:     def __get__(self, obj, cls):
   ....:         return obj.a
   ....:     def __set__(self, obj, val):
   ....:         obj.a = val
   ....:

In [17]: class MyMod(types.ModuleType):
   ....:     b = AGetter()
   ....:

In [18]: mmod = MyMod('my_mod')

In [20]: mmod.__dict__['a'] = 42

In [21]: mmod.a
Out[21]: 42

In [22]: mmod.b
Out[22]: 42

In [23]: mmod.b = 87

In [24]: mmod.a
Out[24]: 87
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20120119/d6486493/attachment.html>


More information about the cython-devel mailing list