Am 29.07.2013 um 18:55 schrieb Dragan Espenschied:
>> user_data = web.ctx.db.select(...) #your query
>> first = next(iter(user_data), None)
>> print(first)
>> 
>> iter(user_data) produces an iterator over the result set, next() then 
>> fetches the next (in this case the first) element of it.
> 
> I thought webpy creates an iterator on db queries by default?
> That is why one has to use .list() to load everything into memory.
> Or am I misunderstanding what you are trying to explain?


web.py creates an iterable containing the results. This prevents all results 
from being loaded into memory, as you mentioned. However, if you use .list() to 
convert that iterable into a list to access the first element, that advantage 
is gone. The complete list has to be loaded into the memory.
To circumvent this, the iterable can be converted into an iterator using 
iter(...). This conversion does not load all results into memory, that's why it 
is preferable to using .list(). From the iterator you can then fetch the first 
element using next(...). 

Best,
Ole.



> 
> Bests,
> Dragan
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "web.py" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/webpy.
> For more options, visit https://groups.google.com/groups/opt_out.
> 
> 

-- 
Ole Trenner
<[email protected]>




-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to