Can I rely on...
MRAB
google at mrabarnett.plus.com
Thu Mar 19 12:01:18 EDT 2009
Emanuele D'Arrigo wrote:
> 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. Take the
> following code:
>
> pattern = "aPattern"
>
> compiledPatterns = [ ]
> compiledPatterns.append(re.compile(pattern))
>
> if(re.compile(pattern) in compiledPatterns):
> print("The compiled pattern is stored.")
>
You don't need parentheses in the 'if', or the 'print' in Python 2.x.
> 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?
>
The re module has a cache of patterns, so if the pattern is already
known then it'll return the existing compiled pattern. However, the
cache has a limited size. In reality, no 2 pattern objects are equal.
> 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?
>
> 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):
>
This is the same as using 'in'.
More information about the Python-list
mailing list