Home > OS >  how to get the values in a class by using a classmethod in python
how to get the values in a class by using a classmethod in python

Time:10-22

This is my first question here, very exciting.

I have a class with a bunch of values attached to it. I would like to get a specific value using a classmethod, or return a list of values. However I just don't seem to be able to get it right.

class StrEnum(str, Enum):
    def __str__(self) -> str:
        return str.__str__(self)

class BunchOfCreatures(StrEnum):
    SPIDERS: str = "Spiders"
    BEETLES: str = "Beetles"
    HORSES: str = "Horses"

    @classmethod
    def get_that_string(cls, BunchOfCreatures: BunchOfCreatures) -> List[str]:
        return [item.value for item in BunchOfCreatures]

What I want to be able to do it say BunchOfCreatures.get_that_string(HORSES), or even a list that includes multiple things, such as HORSES, BEETLES, and have a list containing all the associated string values.

The reason I don't go straight for BunchOfCreatures.HORSES is that I sometimes need to give a list of values to return or compare.

Very grateful for your help.

CodePudding user response:

Welcome to StackOverflow!!!! Here's a way to return any sublist of the creatures. It also has a branch to return ALL if no creatures are specified:

from enum import Enum                                                                                                                 
                                                                                                                                      
class StrEnum(str, Enum):                                                                                                             
    def __str__(self) -> str:                                                                                                         
        return str.__str__(self)                                                                                                      
                                                                                                                                      
class BunchOfCreatures(StrEnum):                                                                                                      
    SPIDERS: str = "Spiders"                                                                                                          
    BEETLES: str = "Beetles"                                                                                                          
    HORSES: str = "Horses"                                                                                                            
                                                                                                                                      
    @classmethod                                                                                                                      
    def list_creatures(cls, creatures=None):                                                                                          
        if creatures==None:                                                                                                           
            return list(map(lambda c: c.value, cls))                                                                                  
        return [c.value for c in creatures]                                                                                       
                                                                                                                                      
                                                                                                                                      
print(BunchOfCreatures.list_creatures())                                                                                              
print(BunchOfCreatures.list_creatures([BunchOfCreatures.HORSES])) 

Note that you do need to append the name of the class (BunchOfCreatures.HORSES) in order to pass something meaningful as an argument. Passing simply HORSES will result in BackTrace (HORSES hasn't been defined outside of the class method!)

  • Related