Can I rely on...

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Mar 20 14:00:42 CET 2009

Emanuele D'Arrigo a écrit :
> Hi everybody,
> I just had a bit of a shiver for something I'm doing often in my code
> but that might be based on a wrong assumption on my part.

Do not assume. Either check or use another solution. My 2 cents...

> Take the
> following code:
> pattern = "aPattern"
> compiledPatterns = [ ]
> compiledPatterns.append(re.compile(pattern))
> if(re.compile(pattern) in compiledPatterns):

you don't need the parens around the test.

>     print("The compiled pattern is stored.")
> As you can see I'm effectively assuming that every time re.compile()
> is called with the same input pattern it will return the exact same
> object rather than a second, identical, object. In interactive tests
> via python shell this seems to be the case but... can I rely on it -
> always- being the case? Or is it one of those implementation-specific
> issues?

I can't tell - I'm not willing to write a serious test for it. IIRC, the 
re module maintains a cache of already seen patterns, but I didn't 
bother reading the implementation. Anyway: why don't you use a dict 
instead, using the "source (ie : string representation) of the pattern 
as key ?


pattern = "aPattern"
compiled_patterns = {}

compiled_patterns[pattern] = re.compile(pattern)

# ...

if pattern in compiled_patterns:
     print("The compiled pattern is stored.")

> And what about any other function or class/method? Is there a way to
> discriminate between methods and functions that when invoked twice
> with the same arguments will return the same object and those that in
> the same circumstances will return two identical objects?

Except reading the source code (hey, this is OSS, isn't it), I don't see 
any reliable way to know this - unless it is clearly documented of course.

> If the answer is no, am I right to state the in the case portrayed
> above the only way to be safe is to use the following code instead?
> for item in compiledPatterns:
>    if(item.pattern == pattern):

Once again, using a dict will be *way* more efficient.

More information about the Python-list mailing list