定义我自己的 None-like Python 常量

2023-10-19Python开发问题
2

本文介绍了定义我自己的 None-like Python 常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一种情况,要求我阅读来自各种来源的数据库更新说明集合.所有源都将包含一个主键值,以便将更新应用于数据库的代码可以找到正确的记录.但是,这些文件会在报告的其他列中有所不同.

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 the code that applies the updates to the database can find the correct record. The files will vary, however, in what additional columns are reported.

当我阅读并创建我的更新说明时,我必须区分提供了列(例如,MiddleName)但为空的更新(意味着没有中间名并且该字段应更新为 NULL)和更新未包含 MiddleName 字段(意味着更新根本不应该触及中间名列).

When I read and create my update instructions I must differentiate between an update in which a column (for instance, MiddleName) was provided but was empty (meaning no middle name and the field should be updated to NULL) and an update in which the MiddleName field was not included (meaning the update should not touch the middle name column at all).

前一种情况(提供了列但没有值)似乎用 None 值适当地表示.然而,对于第二种情况,我想要一个 NotInFile 值",我可以像使用 None 一样使用它.

The former situation (column provided but no value) seems appropriately represented by the None value. For the second situation, however, I'd like to have a NotInFile "value" that I can use similar to the way I use None.

以下是正确的实现方式吗?

Is the correct way to implement this as follows?

NotInFile = 1

class PersonUpdate(object):

     def __init__(self):

         self.PersonID = None
         self.FirstName = NotInFile
         self.MiddleName = NotInFile

然后在另一个模块中

import othermod
upd = othermod.PersonUpdate()
if upd.MiddleName is othermod.NotInFile:
    print 'Hey, middle name was not supplied'

推荐答案

我看不出你的实现有什么特别的问题.但是,1 不一定是最佳标记值,因为它是 Cpython 中的缓存常量.(例如,-1+2 is 1 将返回 True).在这些情况下,我可能会考虑使用哨兵对象实例:

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 还提供了一些其他命名常量,如果您认为合适,您可以使用它们:NotImplementedEllipsis 立即浮现在脑海.(请注意,我不建议您使用这些常量……我只是提供更多选项).

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).

这篇关于定义我自己的 None-like Python 常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10