Unofficial Python Glossary: colloquial Python terminology

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
20 min. read Python 3.8—3.12
Share
Copied to clipboard.
Tags

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

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

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 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.

See What is a mapping? and Creating a mapping for more on how to create your own mappings.

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.

Truthiness (a.k.a. "truth value")

Truthiness asks the question "if we convert this object to a Boolean, will we get True or False?"

All objects in Python have a "truth value": they're either truthy or falsey. All objects are truthy by default unless they define a __bool__ method or __len__ method to customize their sense of truthiness.

In general, truthiness checks for non-zeroness and non-emptiness. Empty collections (such as [], "", ()) are falsey, but non-empty collections are truthy. Numbers that represent zero (0, 0.0, decimal.Decimal("0")) are falsey, but all other numbers are truthy. The value None is also falsey.

See truthiness in Python for more on truthiness.

Lexicographical ordering

Strings, tuples, and lists in Python are ordered "lexicographically".

Lexicographical ordering essentially means "similar to alphabetical ordering". For example, "animal" < "apple" is True because since the first letters are equivalent, the second letters are compared and "n" < "p" (in both Unicode and the alphabet). Likewise, (1969, 5, 18) < (1969, 7, 16) is True because since the first indexes are equivalent, the second indexes are compared and 5 < 7.

See more in the article tuple ordering in Python.

File-like Object (a.k.a. "file object")

A file-like object is an object that behaves like a file.

Files are file-like objects, but so are the standard input (sys.stdin), standard output (sys.stdout), and standard error (sys.stderr) file streams. It's also possible to create your own file-like objects.

See File-like objects in Python for more.

Set-like Object

An object that acts like the built-in set class is a set-like object.

Just as mappings aren't always mutable, set-like objects may not always be mutable either.

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 in 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 a variable 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 recommend 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 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 and Every dunder method in Python.

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.

Hashable

An object that can be passed to the built-in hash function to get its hash value. The hash value of an object is meant to semi-uniquely represent that object.

All objects in a set must be hashable and keys in a dictionary must be bashable.

See what are hashable objects for more.

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 and When is equality the same as identity.

String Representation

An object's string representation determines what happens when Python converts that object to a string.

All Python objects have two possible string representations: the user-readable representation (str) and the human-readable representation (repr). The representation for str defaults to the same representation as repr, so in general most objects only define one of these two string representations: repr.

See customizing the string representation of your Python objects for more on controlling the string representation of your class instances.

Also see the article string representations for classes.

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 (see What's __init__.py). 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). See installing Python packages for more on installable packages.

Standard Library

The modules and packages that are distributed with Python (and to be part of considered) are collectively called "the Python standard library". The Python standard library includes math, datetime, re, sys, pathlib, collections, itertools, functools, statistics, and dozens of other libraries.

The documentation contains a full list of Python's standard library modules. Doug Hellman's Python Module of the Week includes additional explanations of many popular standard library modules.

Third-Party Library

Python packages which are not included with Python (and which you didn't write yourself) are often called third party modules or third-party libraries.

You can install third-party libraries in Python with pip.

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 as a command-line program, from the system command prompt (a.k.a. terminal).

Python scripts are created by making Python modules which are meant to be imported (see making a main function and module vs script).

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 Python 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 for 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).

See using the Python REPL for more.

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 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). For more on this topic, read Callables: Python's "functions" are sometimes classes.

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

An input to a function, a class, or another callable. Arguments can be specified either 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 say "argument" when "parameter" might be more correct).

Decorator

Decorators augment the behavior of functions and classes. Decorators can be applied to a function or class by using an @ symbol to "decorate" that function or class.

Decorators meant for decorating functions are called "function decorators". To make a function decorator, you'll make a function that accepts a function and returns a new function (technically this could be implemented as any callable that accepts a function).

Decorators meant for decorating classes are called "class decorators". To make a class decorator, you'll make a function that accepts a class, modifies that class, and returns the same class (typically).

For more see what is a decorator, how to make a decorator, and making a class decorator.

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). You can make a new class instance by calling the 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 methods, 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 performed 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 creates a new class instance and calls the __init__ method (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 "constructor method". In Python, the 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 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.

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

For a full listing of all 115 dunder methods in Python, see Every dunder method in Python.

Also 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). You can also make an iterator class (see How to make an iterator) but it's unusual to do so.

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

Also see What is an iterator.

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 colloquially 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.

Syntax

The terms are all about Python's "syntax", meaning the symbols, words, and rules that make up valid Python code.

Syntactic sugar

"Syntactic sugar" refers to a programming language syntax that's simply a shortened or improved version of an existing syntax. This phrase usually refers to Python syntax features that are not essential, but are nonetheless helpful.

Python's decorator syntax and list comprehensions are examples syntactic sugar. They make the lives of Python users "sweeter", but we could accomplish the identical tasks even if those special syntaxes didn't exist.

Splat (a.k.a. "star")

The unary * and ** operators in Python are sometimes referred to as "splat" and "double-splat", especially by Ruby programmers. These operators are also sometimes referred to as "star" and "double-star".

Note that this refers to the "unary" or "prefix" * and ** operators, meaning they go before a name (*a or **b) but not in-between two names (a*b or a**b).

Unfortunately, these operators do not have a well-defined name in Python. They also have a wide variety of uses, including accepting any number of positional arguments and accepting arbitrary named arguments.

For more on the unary * and ** operators see the various screencasts and articles on asterisks in Python.

Walrus Operator

The := operator, which is used within an assignment expression.

Python's := operator is often called the "walrus operator" because it looks like a walrus on its side (note the tusks). This operator was added in Python 3.8.

For more, see Python's walrus operator.

Code Style

These terms are all about Python code style.

Duck Typing

A programming style characterized by focusing on the behavior of an object instead of the class (a.k.a. type) of that object.

For example, a function that accepts a writable file-like object might accept any object that has a write method.

Duck typing often works well with an EAFP programming style. For example, if a function accepts a readable file-like object, it may assume the given argument is an iterable of lines and loop over it. If a non-iterable was given, the program would raise an exception, either explicitly or implicitly.

For more, see Duck Typing in Python.

Easier to Ask For Forgiveness Than Permission (EAFP)

EAFP refers to a programming style where an operation is assumed to work, attempted, and then exceptions are handled as needed.

EAFP often involves the use of more try-except blocks and fewer if-else blocks.

EAFP examples include:

  1. Assuming a dictionary key exists and handling a KeyError exception if it doesn't
  2. Assuming a file exists and can be read and handling a number of possible exceptions as needed
  3. Attempting to add a unique value to a database and handling an exception if the value is already in the database

EAFP (Easier to Ask For Forgiveness Than Permission) contrasts LBYL (Look Before You Leap).

Look Before You Leap (LBYL)

LBYL refers to a programming style where assumptions are explicitly checked before moving forward.

LBYL often involves more if-else blocks and fewer try-except blocks.

LBYL (Look Before You Leap) contrasts EAFP (Easier to Ask For Forgiveness Than Permission). LBYL is slightly less common in Python than EAFP because race conditions are less of a concern with EAFP-style programming and asking questions of objects is tricky due to our use of duck typing in Python.

Note that we can often avoid the issue of LBYL vs EAFP entirely by delegating to functions, like the dictionary get method, which hide implementation details from us.

Monkey Patching

Any technique that extends or modifies the behavior of objects, typically objects you wouldn't normally alter, while a Python program is running.

Monkey patching is often used to temporarily replace an object or function, sometimes for the purpose of automated tests. For example, redirect_stdout will temporarily replace the usual standard output stream with a different file-like object.

Practice Python

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.

A Python Tip Every Week

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