__dict__ of object, Was: Regular Expression IGNORECASE different for findall and split?

Steven Bethard steven.bethard at gmail.com
Tue Sep 6 17:42:41 EDT 2005


Chris wrote:
> but more of a basic question following, I was doing the following before:
> 
>     method = 'split' # came from somewhere else of course
>     result = re.__dict__[method].(REGEX, TXT)
> 
> precompiling the regex
> 
>     r = compile(REGEX)
> 
> does give an regex object which has the needed methods
> 
>     print dir(r)
>     ['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 
> 'scanner', 'search', 'split', 'sub', 'subn']
>     
> but how do I evaluate them without explicitly calling them?
> 
>     result = r.__???MAGIC???__[method](TXT)
> 
> obviously I am not a Python pro ;)

Use getattr:

method = 'split'
result = getattr(re.compile(REGEX), method)(TXT)

HTH,

STeVe



More information about the Python-list mailing list