简体   繁体   中英

how to name and create multiple instances of a class in python?

i am new to programming and learning OOP. i wanted to create a deck of playing cards, so i created a class called Card with a number attribute and a suit attribute. then i wanted to create a list of 52 instances of that class to make the deck. i also want each of them to be named like "2_spades", "3_spades", "4_spades" and so on, but obviously dont want to do it manually.

when i created the list using a for loop and printed the list, it printed the memory location of the instances, which was understandable as i hadnt named the instances. so i tried to add the str dunder method to the class, which returned the number and suit of the instance. but it didnt work.

class Card:
    def __init__(self, number, suit, trump='not the trump'):
        self.number = number
        self.suit = suit
        self.trump = trump

    def make_trump(self):
        self.trump = 'the trump'

    def remove_trump(self):
        self.trump = 'not the trump'

    def __str__(self):
        return f'{self.number} of {self.suit}'


suits = ['spades', 'hearts', 'clubs', 'diamonds']
deck = []
for Suit in suits:
    for i in range(13):
        deck.append(Card(i. Suit))

print(deck)

when i print deck, it gives memory locations for each of them.

how can i create multiple instances of the class Card and name each of them by their number_suit or self.number_self.suit?

If you print a list of something the list itself print's it's elements using repr(element) :

class Card: 
    def __init__(self, number, suit, trump='not the trump'):
        self.number = number
        self.suit = suit
        self.trump = trump

    def make_trump(self):
        self.trump = 'the trump'

    def remove_trump(self):
        self.trump = 'not the trump'

    def __str__(self):
        return f'{self.number} of {self.suit}'

    # provide the __repr__ method to be the same as str so iterables
    # when printing this will not print the memory adress but the more
    # meaningfull representation
    def __repr__(self):
        return str(self)   # same as str

should do the trick

You can then simply

suits = ['spades', 'hearts', 'clubs', 'diamonds']
deck = []
for Suit in suits:
    for i in range(13):
        deck.append(Card(i, Suit))   # fix this

print(deck)

Output:

[0 of spades, 1 of spades, 2 of spades, 3 of spades, 4 of spades, 5 of spades, 
 6 of spades, 7 of spades, 8 of spades, 9 of spades, 10 of spades, 11 of spades, 
 12 of spades, 0 of hearts, 1 of hearts, 2 of hearts, 3 of hearts, 4 of hearts, 
 5 of hearts, 6 of hearts, 7 of hearts, 8 of hearts, 9 of hearts, 10 of hearts, 
 11 of hearts, 12 of hearts, 0 of clubs, 1 of clubs, 2 of clubs, 3 of clubs, 4 of clubs, 
 5 of clubs, 6 of clubs, 7 of clubs, 8 of clubs, 9 of clubs, 10 of clubs, 11 of clubs, 
 12 of clubs, 0 of diamonds, 1 of diamonds, 2 of diamonds, 3 of diamonds, 4 of diamonds, 
 5 of diamonds, 6 of diamonds, 7 of diamonds, 8 of diamonds, 9 of diamonds, 10 of diamonds, 
 11 of diamonds, 12 of diamonds]

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