CopyPastor

Detecting plagiarism made easy.

Score: 0.8138523101806641; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2020-09-16
by Norman

Original Post

Original - Posted on 2012-07-10
by glglgl



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Quote from https://docs.python.org/3/reference/compound_stmts.html#function-definitions > 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: penguin = [] penguin.append("property of the zoo") return penguin ```
The relevant part of the [documentation](http://docs.python.org/reference/compound_stmts.html#function-definitions):
> **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: > penguin = [] > penguin.append("property of the zoo") > return penguin >

        
Present in both answers; Present only in the new answer; Present only in the old answer;