Sorting a list of class instances

Grant Ito Grant_Ito at shaw.ca
Wed Dec 25 12:29:11 EST 2002


Hello.

I'm learning to program in Python 2.2.2. I'm trying to simulate a deck of
cards using a list of a Card class that I've created:

import random

class Card(object):
    def __init__(self, val = None):
        self.Pip = val
    def __str__(self):
        return str(self.Pip)
    def __repr__(self):
        return repr(self.Pip)

class Deck(list):
    def __init__(self, numCards):
        self = []
        for i in range(numCards):
            self.append(Card(i))

myDeck = Deck(10)

Instantiating myDeck this way does not work, as I end up with an empty list.
I gather that self is local to the __init__ function. Is it possible to
affect a change in the values of the list this way? I will have to deal with
this again later, as I wish to add a sort function that will sort on Pip.

Many thanks,
Grant.





More information about the Python-list mailing list