Match statement with literal strings

Jason Friedman jsf80238 at gmail.com
Wed Jun 7 20:44:53 EDT 2023


>
> The bytecode compiler doesn't know that you intend RANGE
> to be a constant -- it thinks it's a variable to bind a
> value to.
>
> To make this work you need to find a way to refer to the
> value that isn't just a bare name. One way would be to
> define your constants using an enum:
>
> class Options(Enum):
>     RANGE = "RANGE"
>     MANDATORY = "MANDATORY"
>
> match stuff:
>     case Options.RANGE:
>        ...
>     case Options.MANDATORY:
>        ...
>

Got it, thank you.

On Wed, Jun 7, 2023 at 6:01 PM Greg Ewing via Python-list <
python-list at python.org> wrote:

> On 8/06/23 10:18 am, Jason Friedman wrote:
> > SyntaxError: name capture 'RANGE' makes remaining patterns unreachable
>
> The bytecode compiler doesn't know that you intend RANGE
> to be a constant -- it thinks it's a variable to bind a
> value to.
>
> To make this work you need to find a way to refer to the
> value that isn't just a bare name. One way would be to
> define your constants using an enum:
>
> class Options(Enum):
>     RANGE = "RANGE"
>     MANDATORY = "MANDATORY"
>
> match stuff:
>     case Options.RANGE:
>        ...
>     case Options.MANDATORY:
>        ...
>
> --
> Greg
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list