[ 'pfor item in xprint(itemend='indexing calls __getitem__ for loops call __getitem__ for indexes items in factit' really case of "code oneget bunch free any class that supports for loops automatically supports all iteration contexts in pythonmany of which we've seen in earlier (iteration contexts were presented in for examplethe in membership testlist comprehensionsthe map built-inlist and tuple assignmentsand type constructors will also call __getitem__ automaticallyif it' defined'pin true all call __getitem__ too [ for in [' '' '' '' 'list comprehension list(map(str upperx)[' '' '' '' 'map calls (use list(in (abcdx acd (' '' '' 'sequence assignments list( )tuple( )'join(xand so on ([' '' '' '' '](' '' '' '' ')'spam' in practicethis technique can be used to create objects that provide sequence interface and to add logic to built-in sequence type operationswe'll revisit this idea when extending built-in types in iterable objects__iter__ and __next__ although the __getitem__ technique of the prior section worksit' really just fallback for iteration todayall iteration contexts in python will try the __iter__ method firstbefore trying __getitem__ that isthey prefer the iteration protocol we learned about in to repeatedly indexing an objectonly if the object does not support the iteration protocol is indexing attempted instead generally speakingyou should prefer __iter__ too--it supports general iteration contexts better than __getitem__ can technicallyiteration contexts work by passing an iterable object to the iter built-in function to invoke an __iter__ methodwhich is expected to return an iterator object if it' providedpython then repeatedly calls this iterator object' __next__ method to produce items until stopiteration exception is raised next built-in function is also iterable objects__iter__ and __next__