Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace enum property name

I want to remove underscores in enum name but this code leads to recursion error, how to fix it efficiently?

from enum import Enum

class A(Enum):
    ABC = '0'
    OTC = '1'
    _1T = '2'

    @property
    def name(self) -> str:
        return self.name.replace('_', '')

I came up with this which works:

return self.__dict__['_name_'].replace('_', '')

Is there any other way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try changing:

return self.name.replace('_', '')

to:

return self.__dict__['_name_'].replace('_', '')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading