Congrats to Chris for breaking his PEP curse

Ian Kelly ian.g.kelly at gmail.com
Wed Jul 4 18:23:31 EDT 2018


On Wed, Jul 4, 2018 at 1:11 AM Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>
> On Tue, 03 Jul 2018 23:05:01 -0600, Ian Kelly wrote:
>
> > Now that I've actually read the PEP (sorry, I just assumed it would
> > never fly), I have a couple of (tongue-in-cheek) observations about it:
> >
> >> group = re.match(data).group(1) if re.match(data) else None
> >
> > The only problem with this example of doing more work to save a line of
> > code is that presumably the same programmer would now instead write:
> >
> >> group = match.group(1) if match := re.match(data) else None
>
> There's no stopping some people from making poor naming choices. Surely
> the standard name for match objects is "mo", not match?

I was referring to the variable "match" being referenced spatially
prior to its assignment, not to the naming.

> But if people were going to write that, surely they would now write:
>
>     match = re.match(pattern, data)
>     group = match.group(1) if match else None

Er, I think you're missing the point of the example from the PEP,
which was documenting how sometimes people will make their code do
extra work just to shave off a line. In retrospect, I should have
explained this and not have assumed that the reader would be familiar
with random parts of the PEP.

The assignment expression makes it possible to shave the line without
doing any extra work. Otherwise yes, I agree that this is better
whether you have assignment expressions or not.



More information about the Python-list mailing list