diff options
author | Benjamin Peterson <benjamin@python.org> | 2013-02-10 14:29:59 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2013-02-10 14:29:59 (GMT) |
commit | 1ef876cd28aa2f76edffb6e4d209c6a49b5705ef (patch) | |
tree | eeb6cbf3329f73d4ddce50aef3c912b9e4aef400 /Doc | |
parent | 34a2a87d17ff1730946adf86d23a4737271e53b3 (diff) | |
download | cpython-1ef876cd28aa2f76edffb6e4d209c6a49b5705ef.zip cpython-1ef876cd28aa2f76edffb6e4d209c6a49b5705ef.tar.gz cpython-1ef876cd28aa2f76edffb6e4d209c6a49b5705ef.tar.bz2 |
evaluate positional defaults before keyword-only defaults (closes #16967)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/reference/compound_stmts.rst | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst index d0d0646..c25c767 100644 --- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -493,14 +493,15 @@ case the parameter's default value is substituted. If a parameter has a default value, all following parameters up until the "``*``" must also have a default value --- this is a syntactic restriction that is not expressed by the grammar. -**Default parameter values are evaluated when the function definition is -executed.** This means that the expression is evaluated once, when the function -is defined, and that the same "pre-computed" value is used for each call. This -is especially important to understand when a default parameter is a mutable -object, such as a list or a dictionary: if the function modifies the object -(e.g. by appending an item to a list), the default value is in effect modified. -This is generally not what was intended. A way around this is to use ``None`` -as the default, and explicitly test for it in the body of the function, e.g.:: +**Default parameter values are evaluated from left to right when the function +definition is executed.** This means that the expression is evaluated once, when +the function is defined, and that the same "pre-computed" value is used for each +call. This is especially important to understand when a default parameter is a +mutable object, such as a list or a dictionary: if the function modifies the +object (e.g. by appending an item to a list), the default value is in effect +modified. This is generally not what was intended. A way around this is to use +``None`` as the default, and explicitly test for it in the body of the function, +e.g.:: def whats_on_the_telly(penguin=None): if penguin is None: |