Defining my own None-like Python constant

前端 未结 3 954
独厮守ぢ
独厮守ぢ 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 03:06

    I don't see anything particularly wrong with your implementation. however, 1 isn't necessarily the best sentinel value as it is a cached constant in Cpython. (e.g. -1+2 is 1 will return True). In these cases, I might consider using a sentinel object instance:

    NotInFile = object()
    

    python also provides a few other named constants which you could use if it seems appropriate: NotImplemented and Ellipsis come to mind immediately. (Note that I'm not recommending you use these constants ... I'm just providing more options).

提交回复
热议问题