Home > front end >  How to resolve this circulary dependancy with python enums
How to resolve this circulary dependancy with python enums

Time:12-27

So I have 2 enumerations, the problem is that the first enumration is referencing the second and the second is referencing it too.

    from enum import Enum
    
    class UniverseData:
        def __init__(self, name, coordinate, civilisations)
            self.civilisation_name = name
            self.coordinate = coordinate
            self.civilisation = civilisations
    
    class CivilisationData:
        def __init__(self, name, population_cap, universe)
            self.name = name
            self.population_cap = population_cap
            self.universe = universe
    
    class Universe(Enum):
        Yievdal = UniverseData('Yievdal', 'X258RC', [Civilisation.HUMANITY, Civilisation.ERG, Civilisation.ELF])
        Krastam = UniverseData('Krastam', 'X258BC', [Civilisation.MOJNA, Civilisation.CELES])
    
    class Civilisation(Enum):
        HUMANITY = CivilisationData('humanity', '11B', Universe.Yievdal)
        ERG = CivilisationData('erg', '2B', Universe.Yievdal)
        ELF = CivilisationData('elf', '2B', Universe.Yievdal)
        MOJNA = CivilisationData('mojna', '50B', Universe.Krastam)
        CELES = CivilisationData('celes', '110B', Universe.Krastam)

here the problem is that the Universe enumeration cannot use the Civilisation enumeration since it is declared below.

is there no way around this? I'd like to be able to have the all the universe civilisation in the universe enum.

Thanks.

CodePudding user response:

You could maybe just leave the civilisation attribute in definition and assign it after the Civilisation class has also been defined, this prevents the circular problem. I've shown a possible way but you could just write a function that loops through each attribute of the Universe class and assigns a civilisation.

from enum import Enum

class UniverseData:
    def __init__(self, name, coordinate, civilisations=None)
        self.civilisation_name = name
        self.coordinate = coordinate
        self.civilisation = civilisations

class CivilisationData:
    def __init__(self, name, population_cap, universe)
        self.name = name
        self.population_cap = population_cap
        self.universe = universe

class Universe(Enum):
    Yievdal = UniverseData('Yievdal', 'X258RC')
    Krastam = UniverseData('Krastam', 'X258BC')

class Civilisation(Enum):
    HUMANITY = CivilisationData('humanity', '11B', Universe.Yievdal)
    ERG = CivilisationData('erg', '2B', Universe.Yievdal)
    ELF = CivilisationData('elf', '2B', Universe.Yievdal)
    MOJNA = CivilisationData('mojna', '50B', Universe.Krastam)
    CELES = CivilisationData('celes', '110B', Universe.Krastam)


Universe.Yievdal.civilisation = [Civilisation.HUMANITY, Civilisation.ERG, Civilisation.ELF]
Universe.Krastam.civilisation = [Civilisation.MOJNA, Civilisation.CELES]

CodePudding user response:

In python you cannot reference class's field until you declare class.

So, you need define class Civilisation before usage like this.

from enum import Enum


class UniverseData:
    def __init__(self, name, coordinate, civilisations):
        self.civilisation_name = name
        self.coordinate = coordinate
        self.civilisation = civilisations


class CivilisationData:
    def __init__(self, name, population_cap, universe):
        self.name = name
        self.population_cap = population_cap
        self.universe = universe


class Universe(Enum):
    Yievdal = UniverseData('Yievdal', 'X258RC', [])
    Krastam = UniverseData('Krastam', 'X258BC', [])


class Civilisation(Enum):
    HUMANITY = CivilisationData('humanity', '11B', Universe.Yievdal)
    ERG = CivilisationData('erg', '2B', Universe.Yievdal)
    ELF = CivilisationData('elf', '2B', Universe.Yievdal)
    MOJNA = CivilisationData('mojna', '50B', Universe.Krastam)
    CELES = CivilisationData('celes', '110B', Universe.Krastam)


Universe.Yievdal.civilisations = [Civilisation.HUMANITY, Civilisation.ERG, Civilisation.ELF]
Universe.Krastam.civilisations = [Civilisation.MOJNA, Civilisation.CELES]

  • Related