Python Generator Objects And .join
Solution 1:
join
is a method of strings. That method takes any iterable and iterates over it and joins the contents together. (The contents have to be strings, or it will raise an exception.)
If you attempt to write the generator object directly to the file, you will just get the generator object itself, not its contents. join
"unrolls" the contents of the generator.
You can see what is going with a simple, explicit generator:
defgen():
yield'A'yield'B'yield'C'>>> g = gen()
>>> print g
<generator object gen at 0x0000000004BB9090>
>>> print''.join(g)
ABC
The generator doles out its contents one at a time. If you try to look at the generator itself, it doesn't dole anything out and you just see it as "generator object". To get at its contents, you need to iterate over them. You can do this with a for
loop, with the next
function, or with any of various other functions/methods that iterate over things (str.join
among them).
When you say that result "is a list of string" you are getting close to the idea. A generator (or iterable) is sort of like a "potential list". Instead of actually being a list of all its contents all at once, it lets you peel off each item one at a time.
None of the objects is a "memory address". The string representation of a generator object (like that of many other objects) includes a memory address, so if you print it (as above) or write it to a file, you'll see that address. But that doesn't mean that object "is" that memory address, and the address itself isn't really usable as such. It's just a handy identifying tag so that if you have multiple objects you can tell them apart.
Post a Comment for "Python Generator Objects And .join"