Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23616

pythonwise: Python's iter & functools.partial

$
0
0
Python's built-in iter function is mostly used to extract an iterator from an iterable (if you're confused by this, see Raymond'sexcellent answer on StackOverflow).

iter has a second form where it takes a function with not arguments and a sentinel value. This doesn't see that useful but check out the readline example in the documentation. iter will call the function repeatably until it the function return the sentinel value and then will stop.

What happens when the function  producing the values require some arguments? I this case we can use functools.partial to create a zero arguments function.

Here's an example of a simple HTTP client using raw sockets, we can directly use join on the iterator.


Viewing all articles
Browse latest Browse all 23616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>