I'm curious about why the `copy()` method isn't part of the Sequence and MutableSequence abstract base classes (ABCs) in Python's collections. This isn't about how to work with the absence of this method; I'm looking to understand the reasoning behind that design choice.
5 Answers
Abstract base classes (ABCs) serve to ensure that an arbitrary class implements a necessary set of operations to be treated as a specific type of container. They provide a way to do runtime safety checks on interfaces since Python lacks formal interfaces or declarative typing. This can help during static type checking, especially to differentiate between mutable and immutable types. For example, if you try to add or modify an immutable sequence, the type checker will flag an error.
When designing interfaces, having a method to implement means the implementer has to accommodate all methods. It’s a delicate balance—adding non-essential methods could complicate the interface unnecessarily. That's why `copy()` was likely omitted. However, any class can always add a `copy()` method if needed!
Using `copy()` would mean preserving the original object type, but I've often found it easier to apply duck typing. Instead, I prefer `list()` since it gives me clearer expectations about the results. This approach was also backed by Guido, who prefers `list(x)` over `x.copy()`. It often reflects a more versatile approach.
A sequence in Python can represent multiple structures, even generators, and that raises questions about copying them. Like, if you're copying a generator, do you restart it or continue from where it left off? Many data structures don’t implement `copy()` because it's not always a primary concern, which could have motivated this decision.
The main reason seems to be that collections don't need a `copy()` method to function properly as collections. The developers aimed to keep the interface as minimal as possible. It's interesting because while lists, dictionaries, and sets do have a `copy()` method, the ABCs decided to omit it for the sake of simplicity. Great question by the way!

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically