How to create a set of objects (classes) in Python?

-2

I need to create a set, such as a list, in the Dealer variable, where it needs to contain letter objects, any idea how to do this?

class cartas(object):
        def __init__(self,carta):
            self.carta = carta

        def pontos_carta(self):
            if carta == '1':
                return 1
            elif carta == '2':
                return 2
            elif carta == '3':
                return 3
            elif carta == '4':
                return 4
            elif carta == '5':
                return 5
            elif carta == '6':
                return 6
            elif carta == '7':
                return 7
            elif carta == '8':
                return 8
            elif carta == '9':
                return 9
            elif carta == '10':
                return 10
            elif carta == 'J':
                return 10
            elif carta == 'Q':
                return 10
            elif carta == 'K':
                return 10
            elif carta == 'A':
                return 1
Dealer = []
    
asked by anonymous 03.08.2018 / 19:22

1 answer

0

This example uses a list understanding to create multiple objects of type Carta , one with each possible parameter.

I used the wipe method pontos_carta since you can avoid all those if s using a dictionary of values and int() :

class Carta(object):
    valor_cartas = {
        'J': 10,
        'Q': 10,
        'K': 10,
        'A': 10,
    }

    def __init__(self,carta):
        self.carta = carta

    def pontos_carta(self):
        pontos = self.valor_cartas.get(self.carta)
        if pontos is None: 
            pontos = int(self.carta)
        return pontos


Dealer = [
    Carta(n) for n in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')
]
    
03.08.2018 / 19:34