Python Terminology

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
13 minute read Works on Python 3.7—3.10
Share

An unofficial version of the Python glossary (official glossary) that's meant for average Python users.

The definitions on these page are colloquial and some of these terms never appear in the official Python documentation.

For a list of all terms on this page see All Terms below.

Looping

These terms are all about looping and objects you can loop over.

Iteration

Looping over an iterable object.

A for loop is the epitome of iteration, but there are many other ways to iterate over an iterable in Python.

List comprehensions iterate. Tuple unpacking iterates. Using * operator in a function call iterates (see Unpacking iterables into function arguments). Using * operator in a list literal iterates (see Unpacking iterables into iterables).

Iterable

(Our perspective as Python users) Anything you can loop over with using a for loop or by various other forms of iteration. More on iterables in What is an iterable.

(Python's perspective) Anything that can be passed to the built-in iter function to get an iterator from it. If you're inventing your own iterable class, also see How to make an iterable.

Collections

These terms are all about objects that "contain" other objects (sometimes called collections or data structures). While we use the word "contain" to talk about this relationship, it's not quite the kind of objects nested in objects you might imagine: data structures contain pointers rather than objects in Python.

Sequence (a.k.a. "list-like object")

An ordered collection of objects. Sequences have a length (via the built-in len function), can be indexed (starting at 0), and are iterable.

Sequences can also be thought of as a tuple-like object or a list-like object. Tuples, strings, and lists are all sequences.

See What is a sequence for more on sequences.

Mapping

A dictionary-like object. An object which stores key-value pairs and supports key lookups using square brackets ([...]), among other features we expect dictionary-like objects to support (such as being an iterable). Like sequences, mappings are iterable.

Tuple

An immutable sequence. Tuples act like a list, but they can't be mutated. While tuples could just be thought of as immutable lists, we usually use the two quite differently: tuples are for storing a fixed number of values, often of different types.

For more on the nature of tuples see How to make a tuple and Mutable tuples.

List Comprehension (also set & dictionary comprehension)

A special syntax for building a new list/set/dictionary out of an old iterable. Comprehensions build a new iterable out of an old one while either filtering items down based on a condition or modifying each item based on a mapping expression.

See What are list comprehensions for more on list comprehensions. See Set and dictionary comprehensions for more on set and dictionary comprehensions.

Variables and Assignment

These terms are all about how variables work in Python. There's a level of indirection in Python's variables (through pointers) that makes this concept a bit more complex than it might seem at first glance.

Variable (a.k.a. "name")

A name used to refer to an object. Variables point to objects in Python (see pointers).

For much more on the nature of variables in Python, see Overlooked facts about variables and objects in Python.

Different variables exist is different scopes.

Pointer (a.k.a. "reference")

A sort of "link" to an object, usually a link from a variable to an object or from a data structure to an object.

In Python, variables are pointers (they don't contain objects but point to them). Also, data structures contain pointers in Python.

For much more on the nature of pointers in Python, see Overlooked facts about variables and objects in Python.

Assignment

Assignment points a variable to a value. It's important to note that assignment doesn't copy anything and multiple variables can point to the same value.

Assignments change an objects while mutations change a value (see The 2 types of "change" in Python). Assignments don't usually cause mutations (see Mutating with an assignment statement and Augmented assignments mutate).

We usually think of the = sign when we think of assignment. But for loops, function definitions, and module imports are also assignments (see Assignment isn't just about the equals sign).

For much more on the nature of variables in Python, see Overlooked facts about variables and objects in Python.

Tuple unpacking (a.k.a "multiple assignment")

A special syntax for assigning a name to each item in an iterable. This syntax is usually used with iterables with short iterables that have a known length (most often tuples). See the tuple unpacking article for more.

Attribute

A name that lives on an object. If you see x.y in Python code, y is an attribute of x.

Sometimes the term qualified name is used to describe the full "path" to a name using dotted attribute notation.

For more on attributes, see Where are attributes stored and How attribute lookups and assignments work.

Namespace

A "space" where names live.

This term comes up when discussing:

  • Attributes: a.b accesses the name b within the a object's namespace
  • Variables: a accesses the name a from a namespaces that's currently "in-scope" (see scope)

When discussing variable access, we usually use the word scope instead of the word namespace. The word namespace comes up most often when discussing objects that store attributes (such as modules and classes).

We take namespaces seriously in Python. Somewhat uniquely (not all programming languages have this), each module in Python has a separate namespace (more in Each module has its own global scope). The Zen of Python says "namespaces are one honking great idea -- let's do more of those!"

For a very deep look at namespaces and scope see Python Scopes and Namespaces from the very technical Python Classes Tutorial (I don't this unless you're very interested).

Scope

A place that variables live. When you lookup a variable name, Python first checks the local scope, then the enclosing scopes, then the global scope, and finally the built-ins scope (this name search is sometimes called LEGB). The terms namespace and scope are closely related (scopes rely on namespaces to work) that the term "namespace" is sometimes used when "scope" would work as well.

Unlike many other programming languages, in Python, there's no such thing as "block-level scope".

See local and global variables.

Dunder

Dunder stands for "double underscore". Dunder variables have two underscores on either side: __like_this__.

Dunder variables and dunder methods act define a contract between Python programmers and the Python interpreter. The official Python documentation never uses this phrase: "special attribute" is used instead of "dunder attributes" and "special method" is used instead of "dunder method". You'll sometimes hear Python programmers refer to these as magic methods, though that term seems to have fallen out of fashion.

Example dunder variables include __name__, __doc__, __init__, __repr__, and __eq__. For more, see Dunder variables.

Objects and Mutation

These terms are all about how objects work in Python (note that "object" is a very big term in Python).

Object (a.k.a. "value")

Any "thing" in Python. An object represents data has attributes and methods. The term "object" is tricky to define simply because everything is an object (functions, modules, classes, and class instances are all objects).

Some objects are mutable and some are immutable. Objects cannot contain other objects (see data structures contain pointers).

Mutation

Changing an object. Mutating an object means to change the state of that object (in any way).

Mutating changes an object, while assignment changes a variable (see The 2 types of "change" in Python).

Mutable

An object that can be changed (i.e. mutated). The "value" of a mutable object can change (meaning the data it contains). An immutable object is one that cannot change.

Immutable

An object that cannot be changed (i.e. mutated). The value of an immutable object cannot change. Tuples are examples of immutable objects.

This is a slightly fuzzy term because sometimes the data an object contains can remain constant while its sense of equality changes (see Mutable tuples).

See mutable for mutable objects.

Equality

Whether two objects represent the same data. Equality can be tested with the == operator.

You can control equality on your own objects by defining the __eq__ method.

Also see identity.

Identity

Whether two references/pointers refer to the same object. Identity can be tested with the is operator.

For more on equality versus identity, see Equality vs identity

Modules

These terms are about importing code from other files and creating Python files that are meant to be imported.

Module

A Python file (.py) that's meant to be imported by other .py files. Importing a module runs the code within that module and provides access to a module object which stores that module's functions, classes, and other data.

Variables defined within a module which are not inside a function or class are called global variables (see Local and global variables). Each module's global variables are accessible as attributes on the module object (see Each module has its own global scope).

Python caches modules after they're imported, so importing the same module twice will return the same module object.

Package

A Python module created out of a directory (instead of a single .py file). Python packages are created by making a directory containing a __init__.py file. Python packages can contain submodules (via other .py files within the package directory) and subpackages (via other package directories within the package directory).

It is possible to create a Python package without a __init__.py file, but that creates an implicit namespace package. You shouldn't create namespace packages unless you shouldn't make unless you specifically need a namespace package (they're very rare and you very likely do not need a namespace package).

Not to be confused with an installable Python package, which is a bundle of one or more packages (usually just one) which is usually installed from the Python package index (PyPI).

Scripts and REPL

These terms are about making Python programs and running Python interactively.

Script (a.k.a. program)

A Python file (.py) that is meant to be run directly. Python scripts are usually run from as a command-line program, from the system command prompt (a.k.a. terminal).

Python scripts are created by making a Python modules which is meant to be imported (see making a main function).

Command-line interface

A script or program that is meant to be run from your computer's command prompt (a.k.a. terminal). The command prompt is a program which accepts commands and programs to run.

You can usually start a Python REPL from your command prompt by typing python3, python, or py. Or you can run a Python script by passing a Python filename to the Python command.

Command-line argument

An input to a program. Not to be confused with a function argument, which acts as an input to a function.

Command-line arguments passed to Python can be read from sys.argv (see Accessing command-line arguments).

REPL (a.k.a. interactive Python interpreter)

An environment for repeatedly entering Python statements and seeing the results of those statements.

When you launch Python without giving it any command-line arguments (by typing just py, python, or python3 from your system command-prompt) you'll enter the Pythen REPL. REPL stands for Read Evaluate Print Loop; this describes what Python does as you use the REPL: it reads the statement you've typed, evaluates it when you hit the Enter key, prints out the result of that statement, and then waits you to type another statement again.

The interactive Python interpreter is the official name for the Python REPL, but it goes by many names: Python shell, Python prompt, and Python console are all popular alternate names (in addition to REPL).

Functions

These terms are all about functions and callables.

Function

A way to make reusable code: functions store code to be called later. Functions can have inputs, in the form of arguments, and an output, in the form of a return value. Functions are used by calling them.

Functions in Python are also objects (see Everything is an object).

See Calling a function and How to make a function.

Callable

An object which can be "called". If you have a variable that represents a callable object, you can "call" that object by putting by putting parentheses after it (e.g. like_this()). Any arguments passed to that callable, should be put inside the parentheses.

Functions are callable (calling them runs the code in the function). Classes are also callable (calling them returns a new instance of a class). You can even invent your own callable objects (see What is a callable).

Argument (a.k.a. "function argument")

An input to a function, a class, or another callable. Arguments can be specified positionally or by their name (see positional vs Keyword arguments). An argument can be made "optional" by defining a default value for that argument).

Note that the Python documentation draws a distinction between parameter (the variable name used) and argument (the actual object which will be passed-in to the function), but this distinction is often ignored by Python users (we often to say "argument" when "parameter" might be more correct).

Classes

These terms are all about classes and class instances.

Class (a.k.a. "type")

A way to couple data and functionality. Classes store data within each instance of that class (within the attributes) or each instance. You can make a new class instance by calling it a class. The functionality of a class comes from its methods.

The word "type" is a synonym for class. Popular built-in classes in Python include str, int, float, bool, tuple, list, dict, and set. Every object in Python has a type/class (see Classes are everywhere).

Also see What is a class.

Class instance (a.k.a. "instance")

An object with a specific type. A list object is an "instance" of the list class. You can make a new instance of a class by calling that class.

Class instances store attributes that represent the data for that class instance. Updating an attribute on a class instance changes the data stored with that instance. Actions can be performed on class instances by calling methods on them.

Within a class method, the variable self is usually used to represent the instance that method will operate on (see What is self).

When discussing class instance creation, Python programmers sometimes use slightly jargony Computer Science phrases such as "construct a new instance" or "instantiate the class". Those phrases are more often heard in other programming languages (like Java) but do come up in Python.

Method

A function which is defined on a class and meant to operate on instances of that class. Methods define actions that can be performanced on class instances.

The first argument in every method is usually called self (see What is self). When a method is called on a class instance, the instance will automatically be passed-in as the first positional argument.

See Methods are just functions attached to classes.

Initializer method (__init__)

A method that's called automatically when a new class instance is made.

After calling a class, Python makes a new class instance and calls the __init__ (a dunder method) on that new instance. This method should accept the same arguments that the class accepts.

You'll occasionally hear programmers who moved from other programming languages refer to __init__ as the "construct method". In Python, our constructor method is __new__ and it constructs a new class instance. By the time __init__ is called, the instance already exists (it's passed in as the first argument, self). The __new__ method is rarely defined, so while saying "constructor" might be a bit confusing, most Python programmers will assume you mean the __init__ method.

See What is __init__.

Dunder method

Dunder stands for "double underscore".

The most common dunder methods are __init__, __repr__, and __eq__. See What are dunder methods.

The official Python documentation uses the phrase "special methods" ("dunder" isn't present in the documentation). You'll also sometimes hear Python programmers refer to these as magic methods, though that term seems to have fallen out of fashion.

Also see see dunder variables.

Lazy Looping

These terms are all about objects that generate values as you loop over them. I usually use the term "lazy looping" as an umbrella term for this concept.

Iterator

(Our perspective as a Python user) A lazy iterable that gets consumed as you loop over it. Once all items have been consumed from an iterator, it is exhausted and will appear empty when looped-over again. Iterators can be looped-over (see iteration) or they can be passed to the built-in next function to get just their next item. Generators are iterators and so are many other iterables in Python (see Generators are iterators).

(Python's perspective) Iterators power all forms of iteration in Python (iterables are iterator-powered). See The Iterator Protocol.

Generator expression

A comprehension-like syntax for creating a generator object. You can think of generator expressions as a "generator comprehension"; that term isn't currently used officially or collquially but it probably should be. See How to write a generator expression for more.

Generator function

A function that has one or more yield statements within it. Generator functions do not run when called; instead they return a generator object which will start running the function as it's being looped over. The Python documentation (confusingly in my opinion) sometimes refer to generator functions as simply generators. See What is a generator function for more.

Generator object (a.k.a. "generator")

The return value of a generator function or the result of running a generator expression.

Generator objects (a.k.a. generators) are iterators. Generators are often considered the easiest way to make an iterator. They can also be used for implementing coroutines, though that's usually done using Python's async/await syntax.

Note that I've purposely defined the term "generator" differently than the Python documentation. The Python documentation uses the term generator iterator to refer to a generator object, but the generator to refer to a generator function. From my experience, Python programmers tend to use the term "generator" to refer to generator objects rather than generator functions.

All Terms

Here's a list of every term on this page:

These terms are frequently used throughout Python Morsels exercises, screencasts, and articles. If you're an intermediate-level Python programmer and you'd like an excuse to level-up your skills even more, give Python Morsels a try.

Write more Pythonic code

Need to fill-in gaps in your Python skills? I send regular emails designed to do just that.