Sorting a list of class instances

Denis S. Otkidach ods at strana.ru
Wed Dec 25 12:54:58 EST 2002


On Wed, 25 Dec 2002, Grant Ito wrote:

GI> class Deck(list):
GI>     def __init__(self, numCards):
GI>         self = []

You've created new list here.  To change self in-place use slice
assignment:
            self[:] = []
But you don't realy need this, since constructor starts with
empty list.

GI>         for i in range(numCards):
GI>             self.append(Card(i))
GI>
GI> myDeck = Deck(10)
GI>
GI> Instantiating myDeck this way does not work, as I end up
GI> with an empty list.
GI> I gather that self is local to the __init__ function. Is it
GI> possible to
GI> affect a change in the values of the list this way? I will
GI> have to deal with
GI> this again later, as I wish to add a sort function that will
GI> sort on Pip.

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]





More information about the Python-list mailing list