sec03 - Python None Explained: Meaning, Usage, and Common Pitfalls
スポンサーリンク

What is None

None is a special object that represents “the absence of a value.” Python has the concept of types, and the type of None is NoneType. A key characteristic of None is that only one instance exists during program execution. In other words, no matter how many variables you assign it to, they all point to the same single None. In technical terms, this is called a “singleton” (a design where only one instance of an object exists). Put simply, you can think of it as a “one-of-a-kind entity.”

The following code is an example that demonstrates that only one None exists during program execution. In addition to the basic variable assignments we have studied so far, we will also check the value and id of None assigned within functions and classes. As long as you can confirm that the same id is referenced in all cases, that is sufficient. (Note: the id shown in comments varies depending on the runtime environment.)

(※ We will cover functions and classes in dedicated lectures.)

# Example showing that only one None exists during program execution
# None assigned inside functions, classes, etc. also refers to the same value and id
def check_local_none():
    local_none = None
    print(local_none is None)  # True
    print(id(local_none))      # ID of None (140709436572144)  # id varies by environment
    return local_none  # Return the None assigned inside the function

def return_none():
    return  # Using return to return None

class Sample:
    def get_none(self):
        pass  # If no return is used, None is returned automatically

# None in the global scope
global_none1 = None
global_none2 = None
print(global_none1 is global_none2)   # True (pointing to the same object)
print(id(global_none1))  # 140709436572144 (object ID is the same)
print(id(global_none2))  # 140709436572144

none_from_func1 = check_local_none()
none_from_func2 = return_none()
none_from_class = Sample().get_none()
print(none_from_func1 is None)  # True
print(none_from_func2 is None)  # True
print(none_from_class is None)  # True
print(id(none_from_func1))      # ID of None (140709436572144)
print(id(none_from_func2))      # ID of None (140709436572144)
print(id(none_from_class))      # ID of None (140709436572144)

Using None as a Default Value

One common use of None is to indicate that the value of a variable or an element in a data structure has not yet been determined.

# Initial setup
score = None
print(score)  # None

# Once a score is determined, assign a value
score = 80
print(score)  # 80
d = {}
d.setdefault('key')
print(d)  # {'key': None}  (Since the "value" has not been set yet, None is explicitly used to indicate the absence of a value)
def func():
    pass  # A function that returns nothing

print(func())  # Returns None

In Python, if a function does not include a return statement, it automatically returns None. You can also explicitly write return None, but it has the same meaning.

スポンサーリンク

The Boolean Value of None (Member of Falsy)

When evaluated as a boolean, None always becomes False. Values that evaluate to false in conditional expressions are called falsy. 0 and empty strings "" are also examples of values that evaluate to false in conditionals.

print(bool(None))  # False

The Correct Way to Check for None in if Statements (Using is / is not)

Because only one None exists during program execution, the correct way to check if a variable is None in an if condition is to always use is.

You can also compare with ==, but that checks whether the “contents” of objects are equal, which may lead to unexpected behavior in some cases. Since None evaluates to False, writing if not value: also works (if the variable value is None, the condition evaluates to True). However, this style also evaluates to True when value is 0, an empty string (""), or an empty list ([]). If you want to check specifically whether a value is None, you should always use is None. Likewise, when confirming that a value is not None, use is not None.

value = None

# Checking “if it is None”
if value is None:
    print("The value is None")

# Checking “if it is not None”
if value is not None:
    print("The value is not None")

スポンサーリンク