(not list inherited) Deck class (card games) can be used in for loops, without __iter__ 
Question: 'sup guys,

I'm just doing my usual random little programs before my exam within a few
days and I decided to write a five card draw poker. Since it's simply for
playing a bit with code, I didn't inherit anything from basic classes. My deck
contains a list attribute, which has been given the functionality it needs for
simple readability and functionality:

    
    
    import random
    
    from card import Card
    
    class Deck():
    
        def __init__(self):
            self.cards = self.__generate_cards()
            random.shuffle(self.cards)
    
        def __str__(self):
            cards_str = ""
            for card in self.cards:
                cards_str += str(card)
            return cards_str
    
        def __getitem__(self,index):
            return self.cards[index]
    
        def __generate_cards(self):
            cards = []
            for i in range(4):
                for j in range(13):
                    cards.append(Card(i,j+1))
            return cards
    
        def pop(self,index):
            return self.cards.pop(index)
    

as you can see, there is no `.__iter__` method to be found, which means I
can't iterate over a `Deck()` object right? Python shows me a different story:
for some reason the next code results in looping over the card objects in
`self.cards` and actually printing the cards without the need of an
`iterator`:

    
    
    deck = Deck()
    for card in deck:
        print(card)    # cards have a __str__ method
    

Doesn't make much sense to me, unless it's part of the python magic, giving
any object a chance for possible looping. ;p Can anyone show me the light with
this one? thanks in advance!


Answer: [Why does defining __getitem__ on a class make it iterable in
python?](http://stackoverflow.com/questions/926574/why-does-defining-getitem-
on-a-class-make-it-iterable-in-python)

It's a back-compat thing. Having `__getitem__` makes it iterable.

