Hacker News new | ask | show | jobs
by hermitdev 1739 days ago
It's worth noting that in Python, the enum will be slower than using a bool (how much slower? I don't know - I haven't measured it), if for no other reason than the repeated name lookups. Is it worth fretting over for something that's called occasionally? Probably not. If it's something that's going to be called a lot, e.g. in a tight loop, then it's something to be concerned about.
1 comments

Another solution if you write fully-typed Python is to use typing.Literal:

    calc(x, y, mode="gain")
where the function is defined as

    def calc(
        x: float,
        y: float,
        *,
        mode: Literal["gain", "render"],
    ) -> float:
        ...
This is IMO one of the best things type annotations provide. Combined with Protocol you can write much better structured code with little or no runtime costs.