• 9
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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

      • 1
    • It's perfectly fine for uninitialized, since we don't care about silly corner cases like var1 = (None,) which would pass.
      • 2
    • Yes I said his code was fine, and that var1 = (None,) will PASS this check for being validly initialized (, as in, FAIL the if None in (var1...) comparison. Whereas var1 = None would of course fail the initialized check (i.e. pass the comparison). var1 = (None,) could only ever occur as a hypothetical corner case where var1 was (not properly) initialized by some operation or fn call, I said this case is silly and can be disregarded.

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.

Secondly, just two observations:

The standard way to generate errors in python is to raise 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. 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. 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
  • 13
Reply Report
    • Actually, the proper, final implementation does raise an Exception. Thanks a lot for the insightful answer & observations.

A just slightly different way to do it would be to use the built-in all method; however, this will also catch false-ish values like 0 or "", which may not be what you want:

>>> all([1, 2, 3])
True
>>> all([None, 1, 2])
False
>>> all([0, 1])
False
  • 9
Reply Report

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

  • 7
Reply Report

Trending Tags