how to pass a custom object to

Peter Otten __peter__ at
Fri Aug 17 16:51:48 CEST 2007

 brunovianarezende at wrote:

> I've done that (and added __unicode__ too). I only didn't want to, I
> want to do:
> so, code that worked before as:
> don't have to be changed for:
> and I'm also curious to know if it is possible to do that... :-)

Not without monkeypatching the re module:

import re

_original_compile = re._compile

def _wrapped_compile(*key):
        custom_compile = key[0].__compile__
    except AttributeError:
        return _original_compile(*key)
        return custom_compile(*key[1:])

re._compile = _wrapped_compile

class Aaa(object):
    def __compile__(self, *args):
        return re.compile("[Aa]+")

print re.findall(Aaa(), "a yadda so whaaaat")


More information about the Python-list mailing list