简体   繁体   中英

Python: Finding the name of a enum class

Given the following python code:

class Random_Skew_Enum( Enum ):
    NO_SKEW      = 0
    SKEW_LEFT_01 = 1
    SKEW_LEFT_02 = 2
    SKEW_LEFT_03 = 3
    SKEW_LEFT_04 = 4
    SKEW_LEFT_05 = 5
    SKEW_LEFT_06 = 6
    SKEW_RIGHT_01 = 7
    SKEW_RIGHT_02 = 8
    SKEW_RIGHT_03 = 9
    SKEW_RIGHT_04 = 10
    SKEW_RIGHT_05 = 11
    SKEW_RIGHT_06 = 12

    def _Convert ( self, enumeration ):

        text_to_enumeration = {}
        enumeration_name = ""

        def Do_Convert( self, value ):

            result = text_to_enumeration.get( value )
            if result is None:
                raise CCS_Enumeration_Error( "The enumeration '{0:s}' does not contain the value '{1:s}.".format( enumeration_name, value ) )
            return result

        enumeration_name = enumerate.name
        for enumerate in enumeration:
            match_data = self._split_enumeration.search( enumerate_name )
            new_key = match_data.group( 3 ).upper().strip()
            text_to_enumeration.update( dict( [ new_key, enumerate ] ))
        return Do_Convert

if I then code :

_Convert_Text_To_Random_Skew = self._Convert ( Random_Skew_Enum )

The line

enumeration_name = enumerate.name

generates an exception, I have tried other method to extract the name of the class from the parameter enumeration but with no effect, Can some one help?

The correct attribute to access the name of an object ( if it has one ) is __name__ .

If you want the class name, you would need type(enumerate).__name__ (or enumerate.__class__.__name__ if you're using old-style classes).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM