简体   繁体   English

检查变量之一是否设置为无

[英]Check if one of variables is set to None

I recently had to implement a small check for any variables that might have not been initialized (and their default value is None ).我最近不得不对可能尚未初始化的任何变量(并且它们的默认值为None )进行小检查。 I came up with this:我想出了这个:

if None in (var1, var2, var3):
    error_out()

While, in my eyes, bordering on beautiful, I was wondering - is this a good way to do it?虽然,在我眼里,近乎美丽,但我想知道——这是一个好方法吗? Is this the way to do it?这是这样做方法吗? Are there any cases in which this would produce some unexpected results?在任何情况下,这会产生一些意想不到的结果吗?

First things first: your code is valid, readable, concise... so it might not be the way to do it (idioms evolves with time and new language features) but it certainly is one of the way to do it in a pythonic way.首先要做的事情:你的代码是有效的,可读的,简洁的......所以它可能不是这样做方式(成语随着时间和新的语言特性而发展)但它肯定是一种以pythonic方式实现的方式.

Secondly, just two observations:其次,只有两个观察:

The standard way to generate errors in python is to raise Exceptions .在 python 中生成错误的标准方法是引发 Exceptions You can of course wrap your exception-raising within a function, but since it's quite unusual I was just wondering if you chose this design for some specific reason.您当然可以将异常引发封装在 function 中,但由于这很不寻常,我只是想知道您是否出于某种特定原因选择了这种设计。 Since you can write your own Exception class, even boilerplate code like logging an error message to file could go within the class itself rather than in the wrapping function. Since you can write your own Exception class, even boilerplate code like logging an error message to file could go within the class itself rather than in the wrapping function.

The way you wrote your test is such that you won't be able to assign None as a value to your variables .您编写测试的方式使得您无法将None作为值分配给您的 variables This might be not a problem now, but might limit your flexibility in the future.现在这可能不是问题,但将来可能会限制您的灵活性。 An alternative way to check for initialisation could be to simply not declare an initial value for the variable in question and then do something along the lines of:检查初始化的另一种方法可能是简单地为所讨论的变量声明初始值,然后执行以下操作:

try:
    self.variable_name
except NameError:
    # here the code that runs if the variable hasn't been initialised
finally:
    # [optional] here the code that should run in either case

A just slightly different way to do it would be to use the built-in all method ;一种稍微不同的方法是使用内置的all方法 however, this will also catch false-ish values like 0 or "" , which may not be what you want:但是,这也会捕获像0""这样的虚假值,这可能不是您想要的:

>>> all([1, 2, 3])
True
>>> all([None, 1, 2])
False
>>> all([0, 1])
False

Allow me to leave my two cents here:请允许我在这里留下我的两分钱:

>>> any(a is None for a in [1,0])
False
>>> any(a is None for a in [1,0, None])
True

So one can:所以可以:

def checkNone(*args):
    if any(arg is None for arg in args):
        error_out()

Nothing new here.这里没有什么新鲜事。 Just IMHO maybe the part any arg is None is more readable恕我直言,也许any arg is None的部分更具可读性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM