This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: wrongly cache pattern by re.compile
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ProFatXuanAll, ezio.melotti, mrabarnett
Priority: normal Keywords:

Created on 2020-11-26 18:29 by ProFatXuanAll, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
rebug.py ProFatXuanAll, 2020-11-26 18:29 Bug code
Messages (2)
msg381907 - (view) Author: ProFatXuanAll (ProFatXuanAll) Date: 2020-11-26 18:29
When I run the following code, I expected to get output result `['i am next line with [unk]']`, but instead I get the original list in `data`.

Code snippet

```py
import re

data = [
    '= hello =',
    'i am next line with <unk>',
]

pttn = re.compile(r'=.*=')
samples = filter(lambda sample: not pttn.match(sample), data)

pttn = re.compile(r'<unk>')
samples = map(lambda sample: pttn.sub('[unk]', sample), samples)

print(list(samples))
```

I suspect that is the cache provide by `re.compile` cause the problem.
The `sub` function in `map` is somehow begin link to the first `pttn`.

If I instead rename the second `pttn` to `pttn2`, then it work magically, but this is not expected.
msg381911 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2020-11-26 19:45
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
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86641
2020-11-26 19:45:36mrabarnettsetstatus: open -> closed
resolution: not a bug
messages: + msg381911

stage: resolved
2020-11-26 18:29:35ProFatXuanAllcreate