最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why does pathlib.Path.glob function in Python3.13 return map object instead of a generator? - Stack Overflow

programmeradmin0浏览0评论

I was playing around with Path objects and found an interesting behaviour. When testing with python3.11, Path.glob returns a generator object.

>>> from pathlib import Path
>>> 
>>> cwd = Path.cwd()
>>> cwd.glob("*")
<generator object Path.glob at 0x100b5e680>
>>> import sys;sys.version
'3.11.6 (v3.11.6:8b6ee5ba3b, Oct  2 2023, 11:18:21) [Clang 13.0.0 (clang-1300.0.29.30)]'

I tested the same code with Python3.13, but this time it returns a map object.

>>> from pathlib import Path
>>>
>>> cwd = Path.cwd()
>>> cwd.glob("*")
<map object at 0x101867940>
>>> import sys;sys.version
'3.13.0 (v3.13.0:60403a5409f, Oct  7 2024, 00:37:40) [Clang 15.0.0 (clang-1500.3.9.4)]'

I know the result can be obtained by wrapping it in a list, but I am curios to understand why this change was made. My initial thought was that it might be because of the performance reasons. But a quick test shows that python3.13 version slower than python3.11 on my system.

Here is the benchmark result:

$ python3.11 -m timeit -n 20000 'from pathlib import Path;cwd=Path.cwd();r=list(cwd.glob("*.py"))'
20000 loops, best of 5: 72.9 usec per loop
$ python3.13 -m timeit -n 20000 'from pathlib import Path;cwd=Path.cwd();r=list(cwd.glob("*.py"))'
20000 loops, best of 5: 75.1 usec per loop

I was playing around with Path objects and found an interesting behaviour. When testing with python3.11, Path.glob returns a generator object.

>>> from pathlib import Path
>>> 
>>> cwd = Path.cwd()
>>> cwd.glob("*")
<generator object Path.glob at 0x100b5e680>
>>> import sys;sys.version
'3.11.6 (v3.11.6:8b6ee5ba3b, Oct  2 2023, 11:18:21) [Clang 13.0.0 (clang-1300.0.29.30)]'

I tested the same code with Python3.13, but this time it returns a map object.

>>> from pathlib import Path
>>>
>>> cwd = Path.cwd()
>>> cwd.glob("*")
<map object at 0x101867940>
>>> import sys;sys.version
'3.13.0 (v3.13.0:60403a5409f, Oct  7 2024, 00:37:40) [Clang 15.0.0 (clang-1500.3.9.4)]'

I know the result can be obtained by wrapping it in a list, but I am curios to understand why this change was made. My initial thought was that it might be because of the performance reasons. But a quick test shows that python3.13 version slower than python3.11 on my system.

Here is the benchmark result:

$ python3.11 -m timeit -n 20000 'from pathlib import Path;cwd=Path.cwd();r=list(cwd.glob("*.py"))'
20000 loops, best of 5: 72.9 usec per loop
$ python3.13 -m timeit -n 20000 'from pathlib import Path;cwd=Path.cwd();r=list(cwd.glob("*.py"))'
20000 loops, best of 5: 75.1 usec per loop
Share Improve this question edited Mar 28 at 6:31 user459872 asked Mar 28 at 5:52 user459872user459872 25.3k4 gold badges47 silver badges69 bronze badges 2
  • A map object is a subtype of generator, isn't it? Does the exact concrete type matter when you ultimately want to consume objects generated by it? Also, did you compare the Python source code of those different versions of the pathlib library? – Nayuki Commented Mar 28 at 6:38
  • 1 You measurements are interesting particularly as GH-117586 seems to be all about improving performance – Adon Bilivit Commented Mar 28 at 10:00
Add a comment  | 

1 Answer 1

Reset to default 2

pathlib.Path.glob learned a bunch of new things in Python 3.13, and I suspect the change in return value type from one generator type to another generator was part of that:

Changed in version 3.13: The recurse_symlinks parameter was added.

Changed in version 3.13: The pattern parameter accepts a path-like object.

Changed in version 3.13: Any OSError exceptions raised from scanning the filesystem are suppressed. In previous versions, such exceptions are suppressed in many cases, but not all.

The particular change that made glob() return a map is in this PR (found via git log -S "map(self._from_parsed_string") – you can read the description and commit message there for all the details.

发布评论

评论列表(0)

  1. 暂无评论