Can you reference an enum within itself in Python?

0

I would like to use a constant that I created in one enum as an attribute of another, for example:

from enum import Enum


class Jokenpo(Enum):
    PEDRA = ('Rock', [Jokenpo.PAPEL], [Jokenpo.TESOURA])
    PAPEL = ('Paper', [Jokenpo.TESOURA], [Jokenpo.PEDRA])
    TESOURA = ('Scissors', [Jokenpo.PEDRA], [Jokenpo.PAPEL])


    def __init__(self, nome, fraco, forte):
        self.nome = nome
        self.fraco = fraco
        self.forte = forte

In a stone-paper-scissors game, to specify what stone loses for paper but wins scissors I can do something like that. Probably my enum has some errors, I'm a beginner, but can you use a constant within another?

    
asked by anonymous 01.06.2018 / 04:08

1 answer

0

It has, but not in the way you are doing, although it is not really a problem to create an enumerable from tuples, but the logic itself became quite strange.

Keeping in mind that each value of Enum will be an instance of Enum itself, you can define a property that returns the weakness and strength of each item. For example:

from enum import Enum

class Jokenpo(Enum):
    PEDRA = 1
    PAPEL = 2
    TESOURA = 3

    @property
    def weakness(self):
        if self == self.PEDRA:
            return self.PAPEL
        elif self == self.PAPEL:
            return self.TESOURA
        else:
            return self.PEDRA

print(Jokenpo.PEDRA.weakness)  # Jokenpo.PAPEL
print(Jokenpo.PAPEL.weakness)  # Jokenpo.TESOURA
print(Jokenpo.TESOURA.weakness)  # Jokenpo.PEDRA

See working at Repl.it | Ideone | GitHub GIST

    
01.06.2018 / 13:58