Why is tf.Variable uppercase but tf.constant lowercase?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The naming difference comes from normal Python API conventions, not from a special TensorFlow rule. tf.Variable is a class, while tf.constant is a function that creates and returns a tensor value.
Python Naming Conventions Explain Most of It
In Python, classes are usually written in PascalCase, while functions are usually written in snake_case or lowercase names. TensorFlow follows that convention most of the time, so the capitalization already tells you something about how each API entry is meant to be used.
That means:
- '
tf.Variable(...)constructs an object from a class' - '
tf.constant(...)calls a function that returns a value'
The names look asymmetric, but they are consistent with the role each API plays.
tf.Variable Is a Stateful Object
tf.Variable represents mutable state. It stores a tensor value that can change during execution, which makes it the natural tool for model weights, optimizer state, counters, and any other trainable parameter.
Because it is a class instance, it has methods such as assign, assign_add, and read_value. That object-oriented behavior is one reason the uppercase class name makes sense.
tf.constant Is a Factory Function
tf.constant does not define a class you are expected to instantiate directly. It is a helper function that creates an immutable tensor value from Python data.
The returned object is a tensor, but the public API entry point is a lowercase function because you are asking TensorFlow to build a value for you, not constructing a new user-facing class type yourself.
This style is common across TensorFlow. Many operations are lowercase functions such as tf.reshape, tf.cast, and tf.matmul, even though they may produce complex internal tensor objects.
Why TensorFlow Does Not Use tf.Constant
You could imagine an API with a Constant class, but that would not fit Python's common style for value-creation helpers. TensorFlow exposes tensor-building operations mostly as functions because that keeps the API concise and consistent with the rest of its operator surface.
So the lowercase name does not mean constants are less important. It just reflects that their creation is function-based rather than class-based.
A Practical Mental Model
If you need something that can change, think "object with state" and reach for tf.Variable. If you need a fixed tensor value, think "factory function" and use tf.constant.
That mental model also helps explain why only one of them supports assignment:
Trying to treat a constant tensor like a mutable variable is the real mistake, not the naming difference itself.
This distinction also makes TensorFlow code easier to read once you know the pattern: uppercase APIs often introduce objects, while lowercase APIs often perform operations or create values.
Common Pitfalls
- The capitalization difference is about class versus function, not about importance.
- '
tf.constantreturns a tensor value, but it is still exposed through a factory function rather than a public class constructor.' - New TensorFlow users sometimes expect
tf.Constantto exist because they compare it totf.Variable. - If you need mutation, use
tf.Variable; constants do not support assignment methods.
Summary
- '
tf.Variableis uppercase because it is a class.' - '
tf.constantis lowercase because it is a function that creates a tensor.' - The naming follows standard Python API style more than any TensorFlow-specific rule.
- The easiest way to remember the difference is mutable object versus value-creation helper.

