简体   繁体   中英

Does ' re.finditer() ' store the values it finds in the memory?

import re

txt = 'programming is a beautiful world'

memory =  re.finditer('beautiful', txt)

print(memory)

result:

<callable_iterator object at 0x0000021DB7C1B5B0>

I'm wondering if ' re.finditer() ' store the values it finds in the memory because when i loop over it i see some kind of a memory address i'm really confused or does it use classes?

No, the whole point of an iterator is that it returns one match at a time. The iterator is useful when you want to process one match at a time in isolation, precisely because it avoids returning more than you actually consume. If you bail out after processing the first match, the iterator will never even reach the point of searching for the second.

If you store all the matches in a list in the caller, probably simply use findall instead of finditer , as you are foregoing the possible benefits.

The memory address which Python returns is just the address of the iterator object. Every object in Python has an address in memory, though some are less parsimonious about what exactly is being stored. This is just Python's default repr of internal objects; it includes the memory address mainly as a unique identifer, so that a human reader can tell whether you printed the same object twice, or two different objects of the same type.

Storing the iterator in a variable is sometimes useful, though in this case, it looks like you expected it to contain the matches, not the iterator. This is a common beginner error with iterators and generators.

Understanding the yield keyword should give you a basic grip on generators, and thus iterators. Perhaps review What does the "yield" keyword do?

In so many words, Python keeps track of the state of the iterator between calls. The next time you call it, it recalls where it stopped searching the previous time, and continues searching from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM