summaryrefslogtreecommitdiff
path: root/Doc
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2014-07-06 20:59:19 +0300
committerEzio Melotti <ezio.melotti@gmail.com>2014-07-06 20:59:19 +0300
commit1f6b1034401ef87b490fac3a48c896aad23c9da1 (patch)
treea33407e97698c4712bc6298037aeeb8c06b211bd /Doc
parent1c5f56a955f0aad3105d0b2223b73673d51ffe0f (diff)
parent898eb826962a6d14e92f2f8c4343abeb3f810030 (diff)
downloadcpython-git-1f6b1034401ef87b490fac3a48c896aad23c9da1.tar.gz
#20135: merge with 3.4.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/faq/design.rst56
-rw-r--r--Doc/faq/programming.rst56
2 files changed, 56 insertions, 56 deletions
diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst
index 49e0c6d7de..d7be299a8b 100644
--- a/Doc/faq/design.rst
+++ b/Doc/faq/design.rst
@@ -664,62 +664,6 @@ before you write any of the actual code. Of course Python allows you to be
sloppy and not write test cases at all.
-Why are default values shared between objects?
-----------------------------------------------
-
-This type of bug commonly bites neophyte programmers. Consider this function::
-
- def foo(mydict={}): # Danger: shared reference to one dict for all calls
- ... compute something ...
- mydict[key] = value
- return mydict
-
-The first time you call this function, ``mydict`` contains a single item. The
-second time, ``mydict`` contains two items because when ``foo()`` begins
-executing, ``mydict`` starts out with an item already in it.
-
-It is often expected that a function call creates new objects for default
-values. This is not what happens. Default values are created exactly once, when
-the function is defined. If that object is changed, like the dictionary in this
-example, subsequent calls to the function will refer to this changed object.
-
-By definition, immutable objects such as numbers, strings, tuples, and ``None``,
-are safe from change. Changes to mutable objects such as dictionaries, lists,
-and class instances can lead to confusion.
-
-Because of this feature, it is good programming practice to not use mutable
-objects as default values. Instead, use ``None`` as the default value and
-inside the function, check if the parameter is ``None`` and create a new
-list/dictionary/whatever if it is. For example, don't write::
-
- def foo(mydict={}):
- ...
-
-but::
-
- def foo(mydict=None):
- if mydict is None:
- mydict = {} # create a new dict for local namespace
-
-This feature can be useful. When you have a function that's time-consuming to
-compute, a common technique is to cache the parameters and the resulting value
-of each call to the function, and return the cached value if the same value is
-requested again. This is called "memoizing", and can be implemented like this::
-
- # Callers will never provide a third parameter for this function.
- def expensive(arg1, arg2, _cache={}):
- if (arg1, arg2) in _cache:
- return _cache[(arg1, arg2)]
-
- # Calculate the value
- result = ... expensive computation ...
- _cache[(arg1, arg2)] = result # Store result in the cache
- return result
-
-You could use a global variable containing a dictionary instead of the default
-value; it's a matter of taste.
-
-
Why is there no goto?
---------------------
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index 8dcaa3ad88..71194d0a92 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -352,6 +352,62 @@ the import inside the class but outside of any method still causes the import to
occur when the module is initialized.
+Why are default values shared between objects?
+----------------------------------------------
+
+This type of bug commonly bites neophyte programmers. Consider this function::
+
+ def foo(mydict={}): # Danger: shared reference to one dict for all calls
+ ... compute something ...
+ mydict[key] = value
+ return mydict
+
+The first time you call this function, ``mydict`` contains a single item. The
+second time, ``mydict`` contains two items because when ``foo()`` begins
+executing, ``mydict`` starts out with an item already in it.
+
+It is often expected that a function call creates new objects for default
+values. This is not what happens. Default values are created exactly once, when
+the function is defined. If that object is changed, like the dictionary in this
+example, subsequent calls to the function will refer to this changed object.
+
+By definition, immutable objects such as numbers, strings, tuples, and ``None``,
+are safe from change. Changes to mutable objects such as dictionaries, lists,
+and class instances can lead to confusion.
+
+Because of this feature, it is good programming practice to not use mutable
+objects as default values. Instead, use ``None`` as the default value and
+inside the function, check if the parameter is ``None`` and create a new
+list/dictionary/whatever if it is. For example, don't write::
+
+ def foo(mydict={}):
+ ...
+
+but::
+
+ def foo(mydict=None):
+ if mydict is None:
+ mydict = {} # create a new dict for local namespace
+
+This feature can be useful. When you have a function that's time-consuming to
+compute, a common technique is to cache the parameters and the resulting value
+of each call to the function, and return the cached value if the same value is
+requested again. This is called "memoizing", and can be implemented like this::
+
+ # Callers will never provide a third parameter for this function.
+ def expensive(arg1, arg2, _cache={}):
+ if (arg1, arg2) in _cache:
+ return _cache[(arg1, arg2)]
+
+ # Calculate the value
+ result = ... expensive computation ...
+ _cache[(arg1, arg2)] = result # Store result in the cache
+ return result
+
+You could use a global variable containing a dictionary instead of the default
+value; it's a matter of taste.
+
+
How can I pass optional or keyword parameters from one function to another?
---------------------------------------------------------------------------