Copy's Replace
00:00
In the previous lesson, I showed you the changes to docstring and the new command-line interface to the random
module. This lesson covers the new replace
method in the copy
module.
00:11
copy
module has a new function called replace
, which copies an object but modifies one or more of its values in the copy. For example, say you want to copy a named tuple except for one of its attributes.
00:24
Before, you’d copy it, then make the change. Now you can just use the copy.replace()
function instead. Let’s go look at this. Start by importing copy
, and now to make that named tuple.
00:54 There’s more than one way to create a named tuple. The mechanism I’ve used here is similar to a data class. Now I can create a person and it’ll have three attributes: name, place, and Python version.
01:13
Let’s create a copy of person
, but upgrade the Python version. The replace()
function takes the object being copied, and then the keyword arguments specify what gets changed.
01:29 In this case, it’s the attribute Python version with a new value of 3.13, and it was a copy, so the original hasn’t changed. The original Python version still contains 3.12.
01:43 The logic for handling this copying is available in many of the common objects in the standard library. Let’s play with a date,
01:59
and now if I copy.replace()
it, I can zero out the time part.
02:13
This feature gets handled under the covers using a dunder method called __replace__
. This means you can implement your own version to specify how it behaves. Consider a custom Container
class that has a name and contains a dictionary.
02:34
Here I’m using the keyword args feature of __init__
, which passes any keyword arguments as a dictionary.
02:51
And then I store the name and dictionary for later. The __replace__
method uses **kwargs
to specify the name-value pairs of the things to be replaced.
03:13 Inside the method, I’m responsible for creating the new copy, so I start out by getting an instance of the class, which I’m going to later use for construction.
03:29
Then I create a new dictionary, comprising the existing name and items. The or
on the end here, uses any name-value pairs in the kwargs
dictionary of the function, and overrides the corresponding values in init__kwargs
.
03:45 This gives me a new dictionary with the replaced values. I use this to construct an object returning the new instance based on all of those keyword arguments.
04:02
To top it all off, I’ll implement a __repr__
method showing the contents of the container.
04:29
Alright, let’s create an instance of Container
,
04:43
and now if I copy and replace it, you see the results. There’s two print statements from the __init__
and __replace__
, and then the actual result is printed in the REPL through the __repr__
.
05:01
Notice how the capital of Norway now is capitalized because I changed it using replace()
. pathlib
is one of my favorite modules, it’s been a big improvement on the original os
module.
05:15 Python 3.13 adds more features to it, which I’ll show you in the next lesson.
Become a Member to join the conversation.