[Python-ideas] Python Isn't Perfect: adding a 'gotchas' section to the tutorial

Masklinn masklinn at masklinn.net
Sat Dec 10 15:41:14 CET 2011


On 2011-12-10, at 15:16 , Richard Prosser wrote:
> 
> A classic example is a mutable default argument having the potential to
> produce unexpected side-effects, as a consequence of the non-intuitive
> scoping rules.
As far as I know, mutable default arguments have nothing to do with
scoping, they have to do with the "toplevel" being evaluated fully, so
as the function declaration is evaluated (to create the function
object) so are its default arguments. This is independent from
Python's scoping issues unless I misunderstood what you meant by
"scoping".

But this is definitely something which trips people. However, there is
already a note (though a pretty low-key one, it should probably use an
actual warning directive instead of just bolding it, you should submit
a documentation patch) in the tutorial on that subject[0].

> Another awkward 'feature' is the requirement for a trailing comma in
> singleton tuples, due I believe to the use of expression parentheses rather
> than (say) the use of special brackets like chevrons.
For tuples, there are no matching operators left, as literal sets have
been added.

And technically, the irregularity with tuples is probably the empty
tuple `()` as parens in other tuple arities are only necessary for
disambiguation (much like parens around generator expressions): the
"tuple constructor" is the comma, not the parens,

    a = 1,
    b = 1, 2
    c = 1, 2, 3

are all valid and generate respectively a singleton, a pair and a
triple. In that context, the trailing comma for singletons makes
sense. If you want regularity, you can even add a trailing comma to
the pair and the triple (as you can in e.g. a list or a dict):

    a = 1,
    b = 1, 2,
    c = 1, 2, 3,

I'd rather have a lone comma (with or without parens, depending on the
context) create a null tuple.

> Something that I personally wish for is the ability to declare variable
> types 'up front' but that facility is missing from Python.
I fail to see how this is a "gotcha": since Python is dynamically
typed names don't have types (well technically Python 3 added
documentary type specs to arguments, but they're not used by any
implementation I know of though some third-party tools may already
have started using them)

[0] http://docs.python.org/tutorial/controlflow.html#default-argument-values



More information about the Python-ideas mailing list