Python is readable

Chris Rebert clp2 at rebertia.com
Mon Mar 19 05:14:27 CET 2012


On Sun, Mar 18, 2012 at 8:15 PM, alex23 <wuwei23 at gmail.com> wrote:
> John Ladasky <lada... at my-deja.com> wrote:
>> > The idea that Python code has to be obvious to non-programmers is an
>> > incorrect and dangerous one.
>>
>> Incorrect?  Probably.  Dangerous?  You'll have to explain what you
>> mean.
>
> The classic "obvious" behaviour to new Python programmers that causes
> problems would be mutable default arguments. At this point you have
> two options: improve the "readability" to new users so their
> expectations are met, or ask them to modify those expectations and
> understand what's really happening under the surface. As it stands,
> you tend to hit this problem pretty early on, learn about it, and walk
> away with a deeper knowledge of Python's mechanics.

The mechanics of default arguments maybe, but that's tautological. If
you mean call-by-object, any time you pass both mutable and immutable
things around, you will learn the same lesson. If you mean the more
general principle that "Python doesn't ever copy things unless you
explicitly ask", working with lists (and particularly their
multiplication operator) again tends to also teach that lesson.

> The danger, I
> feel, is that changing it to better fit the mind-set that non-Python
> programmers bring with them could mask that necessary understanding
> for longer.

I disagree. Just add a keyword for each default argument evaluation
behavior (evaluate-once-at-definition-time [e.g. "once", "static"] and
evaluate-at-call-time [e.g. "fresh"]) and don't have there be a
default behavior; make the choice explicit. They'll be confronted with
the issue as soon as they're shown default arguments, and keywords are
easy to look up the meaning of.

And as I said previously, there are other common ways to learn any of
the "lessons" that default arguments might "teach".

If-I-had-my-druthers-ly yours,
Chris (R.)



More information about the Python-list mailing list