[issue42475] wrongly cache pattern by re.compile

Matthew Barnett report at bugs.python.org
Thu Nov 26 14:45:36 EST 2020


Matthew Barnett <python at mrabarnett.plus.com> added the comment:

That behaviour has nothing to do with re.

This line:

    samples = filter(lambda sample: not pttn.match(sample), data)

creates a generator that, when evaluated, will use the value of 'pttn' _at that time_.

However, you then bind 'pttn' to something else.

Here's a simple example:

>>> x = 1
>>> func = lambda: print(x)
>>> func()
1
>>> x = 2
>>> func()
2

A workaround is to capture the current value with a default argument:

>>> x = 1
>>> func = lambda x=x: print(x)
>>> func()
1
>>> x = 2
>>> func()
1

----------
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42475>
_______________________________________


More information about the Python-bugs-list mailing list