[issue29594] implementation of __or__ in enum.auto

Marc Guetg report at bugs.python.org
Sun Feb 19 12:51:27 EST 2017


Marc Guetg added the comment:

One made-up use-case would be:

class LogLevel(Flags):
    start = auto()
    log1 = start | auto()
    log2 = start | auto()


def fun(flags, *args):
    if start in flags:
        # open log file

    if log1 in flags:
       # Log important thing 1

    if log2 in flags:
       # Log important thing 2
    
    if start in flags:
       # close log file


Alternatively the same could be achieved using the existing capabilities with:

class LogLevel(Flags):
     start = auto()
     _log1 = auto()
     log1 = start | _log1
     _log2 = auto()
     log2 = start | _log2

Which is less clear imho and could potentially a problem if somebody uses LogLevel._log2


Another alternative would be that within the function we would check for all cases. eg:

if (start in flags) or (log1 in flags) or (log2 in flags):

Which leads to less clear code and makes the code less maintainable when log3 gets introduced. In the existing case we need to remember to change the if clause both when opening and closing the file. After the proposed change we only need to change the enum.

I'm sure there are more use-cases for it. The one I'm using it for is a bit more convoluted that's why I'm not presenting it here.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29594>
_______________________________________


More information about the Python-bugs-list mailing list