FAQ
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.

Search Discussions

  • Gerson Kurz at Dec 25, 2002 at 5:50 pm

    On Wed, 25 Dec 2002 17:29:11 GMT, "Grant Ito" wrote:
    class Deck(list):
    def __init__(self, numCards):
    self = []
    for i in range(numCards):
    self.append(Card(i))
    The stmt "self = []" is reassigning the variable "self". "self" is not
    a keyword like "this" in python, its just a convention.

    What you want to do is: initialize the list class (the
    "list.__init__(self)" in the example below) instead:

    class Deck(list):
    def __init__(self, numCards):
    list.__init__(self) # <-------- initialize list baseclass
    for i in range(numCards):
    self.append(Card(i))
  • Grant Ito at Dec 25, 2002 at 8:30 pm
    Many thanks, Gerson!

    "Gerson Kurz" <gerson.kurz at t-online.de> wrote in message
    news:3e09ef94.43823093 at news.t-online.de...
    On Wed, 25 Dec 2002 17:29:11 GMT, "Grant Ito" wrote:

    class Deck(list):
    def __init__(self, numCards):
    self = []
    for i in range(numCards):
    self.append(Card(i))
    The stmt "self = []" is reassigning the variable "self". "self" is not
    a keyword like "this" in python, its just a convention.

    What you want to do is: initialize the list class (the
    "list.__init__(self)" in the example below) instead:

    class Deck(list):
    def __init__(self, numCards):
    list.__init__(self) # <-------- initialize list baseclass
    for i in range(numCards):
    self.append(Card(i))
  • Denis S. Otkidach at Dec 25, 2002 at 5:54 pm
    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]

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 25, '02 at 5:29p
activeDec 25, '02 at 8:30p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2021 Grokbase