Defining my own None-like Python constant

前端 未结 3 944
独厮守ぢ
独厮守ぢ 2020-12-04 02:42

I have a situation in which I\'m asked to read collections of database update instructions from a variety of sources. All sources will contain a primary key value so that t

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 02:43

    If you want type-checking, this idiom is now blessed by PEP 484 and supported by mypy:

    from enum import Enum
    
    class NotInFileType(Enum):
        _token = 0
    
    NotInFile = NotInFileType._token
    

    If you are using mypy 0.740 or earlier, you need to workaround this bug in mypy by using typing.Final:

    from typing import Final
    
    NotInFile: Final = NotInFileType._token
    

    If you are using Python 3.7 or earlier, you can use typing_extensions.Final from pip package typing_extensions instead of typing.Final

提交回复
热议问题