How does iter () work?

what is the problem with this code

l=[1,2,3,4,5,6] for val in iter(l, 4): print (val) 

He returns

 TypeError: iter(v, w): v must be callable 

Why does callabe (list) return true , but the called (l) is not working?

EDIT Which method should be used here:

  • manual breaks
  • one hundred others

early

+8
python
source share
6 answers

From iter help:

ITER (...)
iter (collection) β†’ iterator
iter (called, sentinel) -> iterator

 Get an iterator from an object. In the first form, the argument must supply its own iterator, or be a sequence. In the second form, the callable is called until it returns the sentinel. 

You mix the two options for the iter function. The first takes collections, the second takes two arguments - the function and the sentinel value. You are trying to pass a value to validate the collection, which is not true.

Short note: you can get a lot of interesting information from the built-in help function based on python. Just enter python console help(iter) and you will get documentation on it.

Why does callabe (list) return true, but the called (l) is not?

Because list is a function that returns a new list object. The function is callable (what the function does β€” it is called), but the instance that this function returns β€” the new list object β€” does not.

+5
source share

When called with two arguments, iter takes on the value being called and given. This behavior looks like it was implemented like this:

 def iter2args(f, sentinel): value = f() while value != sentinel: yield value value = f() 

What is passed as f must be called, which means you can name it as a function. The built-in list type object that you use to create new instances of the list by calling it as a function:

 >>> list('abcde') ['a', 'b', 'c', 'd', 'e'] 

The list l that you passed in is an existing instance of the list that cannot be used as a function:

 >>> l = [1,2,3,4,5,6] >>> l(3) Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> l(3) TypeError: 'list' object is not callable 

Thus, there is a big and important difference between list objects and list instances that appear when used with iter .

To iterate the list until the watch is reached, you can use itertools.takewhile :

 import itertools for val in itertools.takewhile(l, lambda x: x!= 4): print(val) 
+5
source share

This is due to the fact that the second value is pass (the so-called sentinel value), this ensures that the object to be renamed is called, i.e. function. Therefore, for each iteration that iter() calls __next__() for the object being passed.

iter() has two different behaviors,

  • without sentinel value
  • with a sentinel value

The example in the documentation is great for understanding.

 with open("mydata.txt") as fp: for line in iter(fp.readline, "STOP"): #fp.readline is a function here. process_line(line) 
+4
source share

Take a look at the docs: http://docs.python.org/2/library/functions.html#iter

When the second argument is present in iter , the first argument is handled in a completely different way. This is supposed to be a function that is called at every step. If it returns a sentinel signal (i.e., the second argument), then the iteration stops. For example:

 l=[1,2,3,4,5,6] index = -1 def fn(): global index index += 1 return l[index] for val in iter(fn, 4): print (val) 

EDIT: If you just want to iterate over the list and stop when you see the sentinel, I recommend doing it simply:

 for val in l: # main body if val == 4: break 
+3
source share

Remember that classes are objects in Python.

 >>> callable(list) True 

So this list is a callable, not instances of its callable. As you saw, this is not the case:

 >>> callable([]) False 

In fact, all classes in Python are called - if they don't have literals like list , this is the usual way to create them. Consider:

 def MyClass: pass a = MyClass() 

The last line calls an object of class MyClass , which creates an instance - therefore, MyClass must be called. But you did not expect this instance to be callable, since MyClass itself does not define __call__ .

On the other hand, the MyClass class (i.e. its metaclass, type ) does:

 >>> type.__call__ <slot wrapper '__call__' of 'type' objects> 

what makes MyClass callable.

+3
source share

Why does callabe (list) return true, but called (l) is not?

Since list is a Python built-in function, and l is a list.

+1
source share

All Articles