least-astonishment

“Least Astonishment” and the Mutable Default Argument

一曲冷凌霜 提交于 2019-12-14 03:58:54
问题 Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5] . The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() A manager of mine once had his first encounter with this feature, and called it "a dramatic design flaw"

“Least Astonishment” and the Mutable Default Argument

﹥>﹥吖頭↗ 提交于 2019-12-13 07:14:45
问题 Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5] . The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() A manager of mine once had his first encounter with this feature, and called it "a dramatic design flaw"

“Boolean” operations in Python (ie: the and/or operators)

对着背影说爱祢 提交于 2019-11-27 05:18:26
This method searches for the first group of word characters (ie: [a-zA-Z0-9_] ), returning the first matched group or None in case of failure. def test(str): m = re.search(r'(\w+)', str) if m: return m.group(1) return None The same function can be rewritten as: def test2(str): m = re.search(r'(\w+)', str) return m and m.group(1) This works the same, and is documented behavior; as this page clearly states: The expression x and y first evaluates x ; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. However, being a boolean operator (it even says

“Boolean” operations in Python (ie: the and/or operators)

落花浮王杯 提交于 2019-11-26 12:47:22
问题 This method searches for the first group of word characters (ie: [a-zA-Z0-9_] ), returning the first matched group or None in case of failure. def test(str): m = re.search(r\'(\\w+)\', str) if m: return m.group(1) return None The same function can be rewritten as: def test2(str): m = re.search(r\'(\\w+)\', str) return m and m.group(1) This works the same, and is documented behavior; as this page clearly states: The expression x and y first evaluates x ; if x is false, its value is returned;

“Least Astonishment” and the Mutable Default Argument

岁酱吖の 提交于 2019-11-25 22:09:15
问题 Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function to always return a list with only one element: [5] . The result is instead very different, and very astonishing (for a novice): >>> foo() [5] >>> foo() [5, 5] >>> foo() [5, 5, 5] >>> foo() [5, 5, 5, 5] >>> foo() A manager of mine once had his first encounter with this feature, and called it \"a dramatic design flaw\"