Delete attributes from an enum class in python

Viewed 212

Can i override the delattr or setattr to try to delete or disable some attributes of a an enum class, ex:

Class example(int, Enum):
     S1 = 1
     S2 = 2
     S3 = 3

  def __delattr__():
      # as default, we cannot delete an attribute like S3 from the enum class, but if
      #possible
      # i want this to remove S3

2 Answers

You can't delete attributes from Enums by default because you are not supposed to.

Enums are meant to 'provide a set of symbolic names bound to unique, constant values'

They are meant to be defined within your program and treated as constants.

It's bad practise and can lead to errors where the program expects the existence of that attribute but finds nothing.

Also, please provide some context so we can help suggest alternative methods

Thank you

Apparently, to do so in my use case, it is better to work with cycle structure, provide the updated list and remember the last element in a global variable

import itertools

List = itertools.cycle(Updatedlist)
Related