• <legend id='Ej0aq'><style id='Ej0aq'><dir id='Ej0aq'><q id='Ej0aq'></q></dir></style></legend>
  • <small id='Ej0aq'></small><noframes id='Ej0aq'>

  • <i id='Ej0aq'><tr id='Ej0aq'><dt id='Ej0aq'><q id='Ej0aq'><span id='Ej0aq'><b id='Ej0aq'><form id='Ej0aq'><ins id='Ej0aq'></ins><ul id='Ej0aq'></ul><sub id='Ej0aq'></sub></form><legend id='Ej0aq'></legend><bdo id='Ej0aq'><pre id='Ej0aq'><center id='Ej0aq'></center></pre></bdo></b><th id='Ej0aq'></th></span></q></dt></tr></i><div id='Ej0aq'><tfoot id='Ej0aq'></tfoot><dl id='Ej0aq'><fieldset id='Ej0aq'></fieldset></dl></div>
      <bdo id='Ej0aq'></bdo><ul id='Ej0aq'></ul>

  • <tfoot id='Ej0aq'></tfoot>

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

        Defining my own None-like Python constant(定义我自己的 None-like Python 常量)

        <small id='ItNwp'></small><noframes id='ItNwp'>

            <bdo id='ItNwp'></bdo><ul id='ItNwp'></ul>
              <tbody id='ItNwp'></tbody>
            <legend id='ItNwp'><style id='ItNwp'><dir id='ItNwp'><q id='ItNwp'></q></dir></style></legend>
            • <tfoot id='ItNwp'></tfoot>

              <i id='ItNwp'><tr id='ItNwp'><dt id='ItNwp'><q id='ItNwp'><span id='ItNwp'><b id='ItNwp'><form id='ItNwp'><ins id='ItNwp'></ins><ul id='ItNwp'></ul><sub id='ItNwp'></sub></form><legend id='ItNwp'></legend><bdo id='ItNwp'><pre id='ItNwp'><center id='ItNwp'></center></pre></bdo></b><th id='ItNwp'></th></span></q></dt></tr></i><div id='ItNwp'><tfoot id='ItNwp'></tfoot><dl id='ItNwp'><fieldset id='ItNwp'></fieldset></dl></div>

                  本文介绍了定义我自己的 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 常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                      <tbody id='BJZx8'></tbody>
                  • <tfoot id='BJZx8'></tfoot>
                    <i id='BJZx8'><tr id='BJZx8'><dt id='BJZx8'><q id='BJZx8'><span id='BJZx8'><b id='BJZx8'><form id='BJZx8'><ins id='BJZx8'></ins><ul id='BJZx8'></ul><sub id='BJZx8'></sub></form><legend id='BJZx8'></legend><bdo id='BJZx8'><pre id='BJZx8'><center id='BJZx8'></center></pre></bdo></b><th id='BJZx8'></th></span></q></dt></tr></i><div id='BJZx8'><tfoot id='BJZx8'></tfoot><dl id='BJZx8'><fieldset id='BJZx8'></fieldset></dl></div>

                          • <bdo id='BJZx8'></bdo><ul id='BJZx8'></ul>
                            <legend id='BJZx8'><style id='BJZx8'><dir id='BJZx8'><q id='BJZx8'></q></dir></style></legend>

                            <small id='BJZx8'></small><noframes id='BJZx8'>