two ideoms at one blow: line-reading and regexp-matching

Paul Rubin phr-n2002a at nightsong.com
Thu Feb 21 11:41:34 EST 2002


scarblac at pino.selwerd.nl (Remco Gerlich) writes:
> > Because we've already seen, assignments in expressions are useful.
> > And blurring the statement/expression distinction has proven quite
> > successful in languages like C and Lisp.
> 
> Are you sure? writing 'if (a=3) {' must be the most common bug in C
> programs. I wouldn't call it successful. In fact, Python already caught
> three of those for me this week as syntax errors - that would have been bugs
> in C.

Oh come on, the most common errors in C are probably pointer errors.
Anyway, =/== is a dead horse--didn't we agree that using := for assigments
in expressions totally fixes it?  So there's no reason to keep coming
back to it.

> Also, putting multiple conceptual things on one line isn't good for code
> clarity.

That depends on your style and on the context.

> > Anyway, language attempts to impose "morality" on programmers tend to
> > annoy programmers more than they makes code any cleaner.
> 
> When Python flagged those typos this week, I was *happy*.

If you want Python to catch more typos for you, you're better off
asking for Python to check for variable declarations (I don't mean
static types, I just mean "local x") and flag undeclared variables
like you can with perl's "use strict" option.  That will prevent a lot
more bugs than := is likely to cause.

> > Maybe there's a principle to be found here: "design by FAQ".  If newbies
> > keep arriving and asking over and over again "how do I do X?", maybe
> > there's a good reason for having a way to do X.  We've seen that already
> > for augmented assignments.
> 
> Most newbies tend to come from language X, think Python is great,
> except it isn't quite X - why block indentation when { } would work? Why
> 'self'? Why not assignments in expressions? Etc.

But even oldbies ask about assignments in expressions, augmented
assignment, etc.

> The Python idiom for while (m := re.match('whee',x)): blah() is
> 
> while 1:
>    m = re.match('whee',x)
>    if not m:
>       break
>    blah()
>    
> And to me, an experienced Python programmer who hasn't really used other
> languages in quite a while, that looks *much* clearer than the one liner
> assignment-in-the-while version. It's a common pattern, and the eye
> recognizes it immediately.

And it fills more space on the screen, crowding out other code, so you
can't see as much of the program flow.  I like the short version better.
(Well, I'd probably put the blah() on a separate line).  It's a matter
of style and preference that varies from one programmer to another,
not something the language should impose.  

> But in the end, neither of our subjective opinions matter - Guido's does.
> That's probably why this language is so great.

Guido doesn't write our code; we do.  And we all have our own styles
and preferences that are reflected in the code we write.  I like
Python a lot but I don't think it's perfect, and I don't think anyone
thinks it's perfect.  It changes (mostly for the better) from version
to version, partly based on the shortcomings people find while
programming in it.  If experienced Python programmers regularly lament
the lack of assignment expressions or anything else that's
straightforward to fix, that's probably grounds for considering a fix.



More information about the Python-list mailing list