What is a pure function in Python? It is a function whose return value is determined by its inputs and that causes no observable side effects. Ask two questions: would the same inputs produce the same result, and does calling the function change or interact with anything beyond that returned result? Those tests help distinguish a pure transformation from code that mutates shared data or performs I/O.
What makes a Python function pure?
A pure function computes a result from its arguments and does not produce observable changes elsewhere. The Python Software Foundation’s Functional Programming HOWTO puts the emphasis on avoiding updated data structures and making output depend only on input. In practice, that means considering not just the value returned, but also what the function changes or causes while it runs.
For example, this function transforms a string and returns the result:
def normalize_name(name):
return name.strip().casefold()
For a given string, the result is determined by that input. The function does not print, write a file, change a global variable, or modify the string supplied by its caller. Python strings are immutable, as noted in the Python glossary.
Recommended Free Tools
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
Purity is about effects, not about whether a function contains multiple lines. A function can use local variables and assignments without changing shared state or interacting with the outside world. The HOWTO describes practical functional-style Python as allowing ordinary implementation features while keeping a clear, return-value-oriented interface.
How side effects change the picture
A side effect is an observable result of a call beyond the value it returns. It may be intentional and useful; it simply means the function is not pure.
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
Mutating an input
This function appends directly to the caller’s list:
def add_item(items, item):
items.append(item)
return items
The list has changed after the call, so the result is not just the returned value. Any other part of the program holding that list can observe the update. A return-new-value alternative is:
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
def with_item(items, item):
return [*items, item]
This creates and returns a new list rather than appending to the supplied one. It is an illustration of the difference in behavior, not a claim that one approach is faster.
Printing and other I/O
Calling print() interacts with the screen:
def announce(message):
print(message)
The function’s visible effect is not captured by a return value. The Python HOWTO also names writing to a disk file and calling time.sleep() as side effects. Network requests, database updates, and similar interactions likewise cross the boundary between computation and the outside world.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Reading changing state
A function can also be unpredictable when its result depends on state that is not among its arguments—for example, a mutable global setting that may differ between calls. Even if the function itself does not update that state, its output is not determined solely by its explicit inputs. Making relevant values explicit arguments can make the behavior easier to understand and test.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.How to compare a pure and effectful implementation
When deciding whether a function is pure—or which design best suits a task—look at three practical differences:
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
- Mutation: Does the function alter a list, dictionary, object, or other state that its caller or another part of the program can observe?
- Other effects: Does it print, write a file, sleep, or interact with an external system?
- Test setup: Can a test pass inputs and inspect a returned value, or must it recreate and inspect surrounding state or capture an external effect?
A test of normalize_name(" Ada ") can check the returned string directly. Testing announce() instead requires checking its interaction with output; testing a mutating function also requires checking the state after the call. That extra setup does not make effectful code wrong, but it is a reason to keep effects visible and bounded.
Why use pure functions?
The Python Software Foundation’s HOWTO identifies formal provability, modularity, composability, and easier debugging and testing as advantages of functional design. These are design benefits, not guarantees that a program is correct or faster.
- Predictability: With the same effective inputs, a pure function produces the same result without hidden changes to account for.
- Simpler tests: Tests can focus on inputs and returned values instead of recreating as much external state.
- Easier debugging: Small functions with clear input-output behavior make intermediate values easier to inspect.
- Composability: A return value can be passed to another transformation without coordinating hidden updates between the two operations.
- Modularity: A self-contained transformation can be reasoned about separately from the code that handles files, screens, or other external systems.
As the HOWTO explains, “Functional style discourages functions with side effects that modify internal state or make other changes that aren’t visible in the function’s return value.”
How to use functional style in everyday Python
Python is a multi-paradigm language: a program can be procedural, object-oriented, functional, or a mixture. You do not need to eliminate assignments or make every function pure to benefit from this style. A practical approach is to keep transformations focused on their arguments and return values, then handle necessary I/O and state changes in a small outer layer.
- Keep computation focused. Write functions that accept the values they need and return transformed values.
- Make effects explicit. Put printing, file access, and other interactions in clearly identifiable functions or at the point where they are needed.
- Choose mutation deliberately. If a function changes caller-owned data, make that behavior clear; where useful, return a new value instead.
- Test the boundary. Test pure transformations with inputs and expected outputs, and test effectful operations separately for the changes or interactions they are meant to perform.
Functional Python Programming, Third Edition, by Steven F. Lott is an optional book-length introduction that includes pure functions. Packt lists it as a paperback published in December 2022; its product description says the examples cover Python 3.6, so it should not be treated as a current-version reference.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




