Handling Null values (and equivalents) routinely in Python I've found the following code invaluable in helping me 'handle' None values including "whitespace" characters that should be treated as None based on the situation.   I have been using this code for quite some time now:

class _MyUtils:

    def __init__(self):
        pass

    def _mynull(self, myval, myalt, mystrip=True, mynullstrings=["", "None"], mynuminstances=(int, float)):
        # if the value is None, return the alternative immediately.

        if myval is None:
            return myalt

        # if the value is a number, it is not None - so return the original
        elif isinstance(myval, mynuminstances):
            return myval

        # if the mystrip parameter is true, strip the original and test that
        else:
            if mystrip:
                testval = myval.strip()
            else:
                testval = myval

            # if mynullstrings are populated, check if the upper case of the
            # original value matches the upper case of any item in the list.
            # return the alternative if so.
            if len(mynullstrings) &gt; 0:
                i = 0
                for ns in mynullstrings:
                    if ns.upper() == testval.upper():
                        i = i + 1
                        break
                if i &gt; 0:
                    return myalt
                else:
                    return myval
            else:
                return myval


def main():
    x = _MyUtils()
    print(x._mynull(None, "alternative_value", True, [""]))


if __name__ == '__main__':
    main()


The code requires an input, an alternative to provide if input is found to be Null, whether to 'strip' the input during testing (if not a number), values to treat as 'equivalent' to None and types of number instances to determine if the input is numeric (and hence not none).

Essentially, too many processes that we run depend upon not having None values in the data being processed&mdash;whether that be lambda functions, custom table toolsets, etc.  This code gives me the ability to handle None values predictably, but I am sure there is a better approach here. Is there a more Pythonic way of doing this? How can this code be improved? How would others approach this problem?
Generally I don't think you should have a class for this functionality. There's no state and no particular meaning to MyUtils object here. You can make this into a long function in whatever module you deem appropriate in your codebase.

I think this function as written is a code smell. It 1) doesn't cover a whole lot of types and 2) implies that where you're using it you're not going to have even a rough idea of what type of data you're expecting. In most cases you will have some idea, and even then it's not usually a good idea to do explicit type checking.

Where you're using this for numbers you can replace it with myval if myval is not None else mydefault.

A function like this may be more useful for strings, for which there are a wider range of essentially empty values. Perhaps something like this

def safe_string(s, default="", blacklist=["None"]):
    if s is None or len(s.strip()) == 0:
        return default
    if s.upper() in [b.upper() for b in blacklist]:
        return default
    return s