MarkF207
MarkF207

Reputation: 121

Python printing before function call returns?

ls = get_details(urls)
print "wtf..."
print "wtf..."
print "wtf..."

get_details() is doing a bit of screenscraping with xpath, it returns/generates a generator of about 200 lists containing ~10 pieces of profile information.

Why is it executing the print statements before the function returns...??? I have quadruple checked it for silly indent errors etc...

EDIT: I'm sorry folks I can't post the code. But a print statement on line 2 executing before the function on line 1 returns... is new to me.

FIX: OK so, apparently I had to "exhaust" the generator after I returned it... didnt know that, what else don't I know about generators ^.^

Thanks for the help guys!

Upvotes: 2

Views: 206

Answers (2)

falsetru
falsetru

Reputation: 369074

Generator need to be exhausted before it return.

gen = get_details(urls)
for x in gen:
    # do something with x
print "wtf..."
print "wtf..."
print "wtf..."

Upvotes: 2

Devansh Agarwal
Devansh Agarwal

Reputation: 126

How do you know it executing the print statements before the function return the value. Do

ls = get_details(urls)
print ls
print "wtf..."

and post the answer.

Upvotes: 1

Related Questions