Created
May 5, 2022 11:46
-
-
Save iaindillingham/f399f55e2dc5a8e2d1bfc094836b7642 to your computer and use it in GitHub Desktop.
Generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import pathlib | |
def match_paths_1(pattern): | |
# not a generator function | |
return (pathlib.Path(path) for path in glob.iglob(pattern)) | |
def match_paths_2(pattern): | |
# a generator function | |
for path in glob.iglob(pattern): | |
yield pathlib.Path(path) | |
def match_paths_3(pattern): | |
# a generator function | |
yield from (pathlib.Path(path) for path in glob.iglob(pattern)) | |
my_pattern = "output/*.csv" | |
match_paths_1(my_pattern) | |
# <generator object match_paths_1.<locals>.<genexpr> at 0x1046e7f90> | |
match_paths_2(my_pattern) | |
# <generator object match_paths_2 at 0x1046e7f20> | |
match_paths_3(my_pattern) | |
# <generator object match_paths_3 at 0x103e66900> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment