Creating object with a loop

I am building a variation of poker that my family and I play and I’m trying to see if there is a way to quickly create card objects with variable names that are intuitively referenced when playing a hand. This is what I have so far:

numbers = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace']
suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']


class Card:
    def __init__(self, suit, number):
        self.suit = suit
        self.number = number

        if self.suit == 'Spades':
            suit_rank = 1
        elif self.suit == 'Hearts':
            suit_rank = 2
        elif self.suit == "Clubs":
            suit_rank = 3
        elif self.suit == 'Diamonds':
            suit_rank = 4
        self.suit_rank = suit_rank


    def __repr__(self):
        description = "{number} of {suit}".format(number = self.number, suit = self.suit)
        return description

class Deck:
    def __init__(self):
        self.cards = []
        for suit in suits:
            for num in numbers:
                self.cards.append(Card(suit, num))
    def show(self):
        for c in self.cards:
            print (c)

The problem is these card objects are not easily called without knowing the index of where they fall in a player’s hand. I would like each cards object to be assigned to a variable that is named in such a way that the players can reference them by typing out something like “two of hearts.”

So I guess my question is, is there a way to loop through the list of numbers and suits to simultaneously create the objects and assign the variable name so that the objects are intuitively called? It seems tedious to have to go through and create each variable one by one.

I hope my question makes sense!

I seem to recall the book fluent python having an example like this near the start which might be useful to you if you can get your hands on a copy.

What you’re describing though sounds more like a mapping of some kind. So could perhaps make use of a dictionary, inherit from one for your new collection or implement dictionary-like methods (.__getitem__ and similar see the mapping abstract types for help).

I think that’s what you’re after but by all means say so if not.

Overwriting .__getitem__ would give you the most options (this is the method that’s called when you use the subscript syntax object[subscript]).

To help point you in the right direction:

Take a look at the Python Documentation on itertools.combinations() - it creates a tuple based on two lists. You will need to import the module itertools in order to call upon combinations. From the tuple you could create a print statement from ‘2 of Spades’ or ‘Ace of Hearts’

I will also call something two your attention now, you will have to also define the iteratable within your class: init and next because classes are not iterable by default in Python. (Built-in Types — Python 3.10.2 documentationnext#iterator.next).

There is a course on Intermediate Python 3 on Courseacdemy that delves deeper into this topic and I highly recommend taking it for your project, it s helped me a lot.