Curious Minds

Clear answers to science and history questions

Why Does Range Return a Function in Python?

· 4 min read · Science

Range returns a function in Python because it is not a function itself, but rather a built-in class that returns an immutable sequence object designed to generate numbers on demand. When you call range(), you're creating an instance of the range class, which behaves like a function call but actually returns a range object optimized for memory efficiency and iteration.

Background: The Evolution of Range in Python

The range type has evolved significantly between Python 2 and Python 3, which is key to understanding its current behavior. In Python 2, range() returned a list of integers, which consumed memory proportional to the size of the range. For example, range(1000000) would create a list with one million integers in memory immediately. This was inefficient for large ranges and common use cases like looping.

To address this, Python 3 redefined range as a class that returns a sequence object generating values lazily—only when iterated over. This change preserved the familiar syntax while drastically improving performance and memory usage. The range object is not a function, but calling it with parentheses mimics function syntax, leading to the common misconception that it is a function.

How Range Works Internally

When you call range(start, stop, step), Python creates a range object that stores only three values: start, stop, and step. It does not precompute or store the entire sequence. Instead, it calculates values on-the-fly during iteration using a mathematical formula:

  • The first value is start
  • Each subsequent value is start + step * n, where n is the iteration count
  • Iteration stops when the value meets or exceeds stop (depending on step direction)

This allows range(0, 10**12) to be created instantly and use minimal memory, even though it represents a trillion numbers. The object supports indexing, length calculation, and membership testing without generating all values.

Range Is a Class, Not a Function

Despite being called with parentheses like a function, range is actually a built-in class. You can verify this in Python:

  1. Use type(range) → returns <class 'type'>, indicating it’s a class
  2. Use type(range(5)) → returns <class 'range'>, confirming the return value is a range object
  3. Call range(5) → returns range(0, 5), a readable representation of the object

Because it’s a class, range supports methods like __len__(), __getitem__(), and __contains__(), enabling operations like len(range(10)) or 5 in range(10) without iteration.

Why the Syntax Mimics a Function

Python uses callable syntax—parentheses—for both functions and classes. Since range is a class with a __call__ method (via its type), it can be instantiated using range(). This design choice maintains backward compatibility with function-like usage while enabling object-oriented benefits.

The illusion of a function is intentional and practical:

  • It preserves the familiar, clean syntax used in loops: for i in range(10):
  • It avoids introducing new syntax for a common operation
  • It aligns with other built-in types like list() or tuple(), which are also classes but look like functions

This pattern is common in Python: int(), str(), dict() are all classes that return objects when called.

Memory and Performance Benefits

The primary advantage of range returning an object instead of a list is memory efficiency. Consider these examples:

  • range(1000) uses about 48 bytes of memory, regardless of size
  • list(range(1000)) creates a list with 1000 integers, consuming ~8 KB or more
  • For range(10**9), the range object still uses ~48 bytes; the list would require several GB

Performance gains are equally significant:

  1. Creating a range object is O(1) — constant time
  2. Accessing an element by index is O(1) — direct calculation
  3. Iterating is O(n), but with minimal overhead per step

Contrast this with Python 2’s range(), where creating a list was O(n) in both time and memory.

Common Misconceptions and Pitfalls

Several misunderstandings arise from the function-like appearance of range:

  • Misconception: range() returns a list — it does not in Python 3
  • Misconception: range is a function — it’s a class
  • Pitfall: Assuming range generates all values upfront — it does not
  • Pitfall: Using range with non-integer arguments — raises TypeError

A key point: because range objects are lazy, they cannot hold mutable or non-numeric types. They are strictly for integers and are immutable once created.

Frequently Asked Questions

Is range a function in Python?

No, range is not a function. It is a built-in class. While it is called using function-like syntax, it returns an instance of the range class, not the result of a function.

Why does range() look like a function but isn’t?

Because in Python, classes can be callable. The range class is instantiated using parentheses, just like list() or int(). This syntax is consistent and user-friendly, even though it’s not a function.

Does range store all the numbers?

No. A range object stores only the start, stop, and step values. It calculates individual numbers on demand during iteration or indexing, which saves memory.

Can I use floats in range?

No, range only accepts integers. For floating-point sequences, use numpy.arange() or a generator expression like (x * 0.1 for x in range(0, 10)).

How is range different from xrange in Python 2?

In Python 2, xrange() was the memory-efficient version that returned an iterator, while range() returned a list. In Python 3, range() replaced xrange() and behaves like the old xrange.

Can I modify a range object?

No. Range objects are immutable. Once created, you cannot change their start, stop, or step values. You must create a new range instance to alter them.

In this article
  1. Background: The Evolution of Range in Python
  2. How Range Works Internally
  3. Range Is a Class, Not a Function
  4. Why the Syntax Mimics a Function
  5. Memory and Performance Benefits
  6. Common Misconceptions and Pitfalls
  7. Frequently Asked Questions