简体   繁体   中英

__str__ method on list of objects

New to python, trying to create a card deck and want to implement a printing method for print(deck) => that gives a printed list of my cards.

My class PlayingCard has a str method which works fine to print a single card.

But when I create my Deck.cards object (which is a list of PlayingCard objects), I can't seem to print all the cards at once.

Tried replacing str with repr . Tried returning a list of strings in str

from enum import Enum

class Value(Enum):

    Two = 2
    Three = 3
    Four = 4
    Five = 5 
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = 11
    Queen = 12
    King = 13 
    Ace = 14

class Suit(Enum):

    Spades = 1
    Hearts = 2
    Clubs = 3
    Diamonds = 4


class PlayingCard():

    def __init__(self,value,suit):
        self.value = value
        self.suit = suit


    def __str__(self):
        return '{} of {}'.format(Value(self.value).name, Suit(self.suit).name)

class Deck():

    def __init__(self):
        self.cards=[PlayingCard(val,suit) for val in Value for suit in Suit]

    def __str__(self):
        return str(self.cards)


deck = Deck()
print(deck)

Returns

[<__main__.PlayingCard object at 0x000001875FEC0080>,
<__main__.PlayingCard object at 0x000001875FE9EE48>,
....
....
]

New to python, trying to create a card deck and want to implement a printing method for print(deck) => that gives a printed list of my cards.

My class PlayingCard has a str method which works fine to print a single card.

But when I create my Deck.cards object (which is a list of PlayingCard objects), I can't seem to print all the cards at once.

Tried replacing str with repr . Tried returning a list of strings in str

from enum import Enum

class Value(Enum):

    Two = 2
    Three = 3
    Four = 4
    Five = 5 
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = 11
    Queen = 12
    King = 13 
    Ace = 14

class Suit(Enum):

    Spades = 1
    Hearts = 2
    Clubs = 3
    Diamonds = 4


class PlayingCard():

    def __init__(self,value,suit):
        self.value = value
        self.suit = suit


    def __str__(self):
        return '{} of {}'.format(Value(self.value).name, Suit(self.suit).name)

class Deck():

    def __init__(self):
        self.cards=[PlayingCard(val,suit) for val in Value for suit in Suit]

    def __str__(self):
        return str(self.cards)


deck = Deck()
print(deck)

Returns

[<__main__.PlayingCard object at 0x000001875FEC0080>,
<__main__.PlayingCard object at 0x000001875FE9EE48>,
....
....
]

New to python, trying to create a card deck and want to implement a printing method for print(deck) => that gives a printed list of my cards.

My class PlayingCard has a str method which works fine to print a single card.

But when I create my Deck.cards object (which is a list of PlayingCard objects), I can't seem to print all the cards at once.

Tried replacing str with repr . Tried returning a list of strings in str

from enum import Enum

class Value(Enum):

    Two = 2
    Three = 3
    Four = 4
    Five = 5 
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = 11
    Queen = 12
    King = 13 
    Ace = 14

class Suit(Enum):

    Spades = 1
    Hearts = 2
    Clubs = 3
    Diamonds = 4


class PlayingCard():

    def __init__(self,value,suit):
        self.value = value
        self.suit = suit


    def __str__(self):
        return '{} of {}'.format(Value(self.value).name, Suit(self.suit).name)

class Deck():

    def __init__(self):
        self.cards=[PlayingCard(val,suit) for val in Value for suit in Suit]

    def __str__(self):
        return str(self.cards)


deck = Deck()
print(deck)

Returns

[<__main__.PlayingCard object at 0x000001875FEC0080>,
<__main__.PlayingCard object at 0x000001875FE9EE48>,
....
....
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM