Nested Replacement Fields
One of the coolest things about these replacement fields is that they can be nested within one another. So you can define things like the format specification in terms of other arguments that are passed into the
00:13 If you’re not sure what I mean, stick with me and I’ll go through it. So as I said, replacement fields can be created dynamically. Their names, conversions, and format specs are all totally fair game.
00:24 This allows you to create functions and classes that can format their own output in whatever way best suits your data. As you can imagine, this is a hugely convenient feature, especially if you’re creating processes that are meant to run in the command line, which is the text-based program that a lot of powerful utilities use to run.
Okay. Let’s take a look at how these nested replacement fields work. If you remember, the previous version of this was I could just say something like, “Well, maybe I want to print out a number and maybe I want to fill with a tilde, right-aligned, and using an minimum width of
8.” And that’s all well and good and I can format the number
1 there, or the number
4, and it will work just fine. Anything up to eight digits will be great, so that’ll be all good.
But it would be really cool if I could actually do something more interesting here and make these fields dynamic, right? So maybe I could make this minimum width passed in to
.format(), and same with tilde, perhaps. And it turns out you can do just that—you just have to be careful about your indices.
I’m going to say “Format the zeroth entry in the arguments with the
<fill> character provided by the first member of the arguments, and the
<width> provided by the actual second member of the arguments here—or the argument at index
2, I should say.
'qqqqqqqq1234'. This is pretty sweet because then if I wanted to, I could actually wrap this in a function. I could define
qify() and it would take in a
num (number), a
fill_char (fill character), and a
well, I should say
min_width just so that I’m really careful here. And then that would just return what I have up here—let’s see, okay. I’m just going to have to type it out again. That’s how it goes sometimes.
So I’m going to say
0—and so I just replace these actual numbers and characters up here with the passed-in values. This is really nice. And I have to remember to actually return the value here because that’s how functions work—you know how it goes. And so once I do that, though, and I go and click Enter here, then I can
qify()—or I can really choose anything that I want there, so I could say
1. And I could say—let’s keep it consistent—
min_width, let’s give it
03:08 So the power of this idea is really, really something, and you just have to remember that you have to be really careful about your indices or your keywords—because you can also use keywords here.
03:33 So I would recommend using keyword arguments when you’re using nested replacement fields, because they just make things a lot easier to read for someone else who’s coming in to look at your code, or even for you when you come back to look at it.
So, that is the super powerful, flexible, nested replacement field, and keep in mind that you can nest as many times as you want. You just have to be careful about those indices and keywords. Next up, I’ll talk about f-strings, which are a slightly more nuanced and less verbose version of this
.format() function that can be really useful in a lot of contexts.
Become a Member to join the conversation.