HOWTO · Python

How to Declare a Variable Without Value in Python

Learn how Python treats an unbound name, a `None` placeholder, an annotation without assignment, and empty string or list values.

On this page

To prepare a variable for a value you will provide later, assign None as a placeholder. Python binds a name only when an assignment runs; simply mentioning a name or writing an annotation does not give it a runtime value. Use a variable annotation when you need to record a type without assigning, or assign an empty string or list when that is the value you want to start with.

How Python handles a variable with no assigned value

In Python, assignment creates a binding between a name and an object. A name that has not been assigned in the current scope is unbound. Reading it raises NameError; it is not a variable holding a special “nothing” value. This distinction matters when choosing how to represent data that will be supplied later.

The options below create different states: None is an assigned placeholder, an annotation records type information without assigning a value, and an empty string or list is an assigned object that can be used immediately.

Assign None as a placeholder

Assign None when the name should exist now, but the meaningful value is not available yet. None is Python’s singleton object for representing absence; assigning it binds the name, so it is different from leaving the name unbound. Check for it with is None.

Run this complete example to compare the three choices:

profile = None
message = ""
items = []

if profile is None:
    profile = {"name": "Ada"}

message += "Hello"
items.append("first")

print(profile["name"])
print(message)
print(items)

After the check, profile refers to a dictionary. A None parameter default distinguishes an omitted argument from a supplied non-None value, because the supplied value replaces the default. It cannot distinguish omission from an explicit None; use a unique sentinel object as the default when those calls must be treated differently.

Use a variable annotation without assigning a value

A variable annotation can record the intended type without binding a runtime value to the name:

name: str

This is an annotation, not an assignment. The annotation may be available through the containing module or class’s annotation data, but reading name before assigning it still raises NameError. If later code needs to read the value, assign one first, such as name = "Ada". Use annotation-only syntax when documenting the intended type is useful and assignment should happen elsewhere.

Use an empty string or list when an empty value is intended

Assign an empty value when it is already the correct starting value for your task. An empty string is still a string, and an empty list is still a list; neither represents an unbound name or a None placeholder.

The example above starts message as an empty string and items as an empty list, then adds content. Choose the type that matches the data you plan to store. A list can be populated later with methods such as append() or extend().

Choose the form that matches your intent

  • Use None when absence is meaningful and the name should already be bound. Check it with is None before replacing or using the placeholder.
  • Use an annotation such as name: str when you need to record the intended type but do not want to assign a value at that point. It does not make the name readable at runtime.
  • Use "" or [] when an empty string or collection is the actual initial value your code needs.

If a read raises NameError, first check whether an assignment has run in that scope. Assign None if you need a placeholder, or initialize the value your code will use. Do not use annotation-only syntax as a substitute for runtime initialization.