Why is this not an instance method and __iter__ is?

The "intuitive" way to get an iterator for those who usually run programs in Java, C ++, etc., is something like list.iterator().

Why did Python people decide to use it as a generic type function len()(which leads to iter(list), rather than to list.iter())?

The same question can be asked for the length of the structure ( len()).

+4
source share
1 answer

iter() supports various types of objects.

( ), ( , obj.__iter__()) ( self __iter__).

Java list.iter() list.__iter__() Python, iter() . __iter__, , .

, :

iter(fileobj.readline, '')

, readline(), ( , ).

; iter() API- , ; , , obj.iter() obj.iterator() obj.get_iterator().

+4

All Articles