Iterables: A Crazy, Crazy World
a5rocks (535)

Part 1: Well, what are iterables?

My personal definition of Python's iterables is just that they are special functions. That's it.

An example of one would be this basic and simple clone of range:

def simple_range(start=0, max=10):
	i = start
	
	# This is the real logic here
	while i < max:
		yield i
		i += 1

Now, how would we use this "special" function? Well, you can just put it in a for loop:

for i in simple_range(0, 4):
	print(i)

This will output:

0
1
2
3

What's so special about iterables then? Couldn't you just do that with a normal function and a list?

Well, yes, but I have a reason for naming this series "A Crazy, Crazy World."

In the console / REPL, try the following:

> f = simple_range(0, 4)
> dir(f)
[..., 'close', 'gi_code', 'gi_frame', 'gi_running', 'gi_yieldfrom', 'send', 'throw']

Wait a second. WHAT ARE THOSE RANDOM FUNCTIONS?

(Click here to go to the next tutorial.)

(I hope this made you interested in iterables. And believe me, they are quite the cool thing once you learn about them and what you can do with them.)

You are viewing a single comment. View All
pyelias (923)

nice, these are definitely an interesting feature
<pedantry>technically you're describing generators here, which are just one type of iterable</pedantry>