From 723fe5fd8a0d3d3f9a983365d912f5d5042da17c Mon Sep 17 00:00:00 2001 From: Jiajie Zhong Date: Mon, 6 Jul 2020 17:00:45 +0800 Subject: [PATCH 1/7] Doc: Add output hint in faq programming --- Doc/faq/programming.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 61ffc5dbdaa773..24d0acc99414e3 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -549,12 +549,12 @@ desired effect in a number of ways. args = {'a': 'old-value', 'b': 99} func3(args) - print(args['a'], args['b']) + print(args['a'], args['b']) # output: new-value 100 5) Or bundle up values in a class instance:: class callByRef: - def __init__(self, /, **args): + def __init__(self, **args): for key, value in args.items(): setattr(self, key, value) @@ -564,7 +564,7 @@ desired effect in a number of ways. args = callByRef(a='old-value', b=99) func4(args) - print(args.a, args.b) + print(args.a, args.b) # output: new-value 100 There's almost never a good reason to get this complicated. From da3cbb65710688138c03693c428fad7dc13ef00d Mon Sep 17 00:00:00 2001 From: Jiajie Zhong Date: Fri, 10 Jul 2020 21:20:11 +0800 Subject: [PATCH 2/7] Use console style and change class name --- Doc/faq/programming.rst | 72 ++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 24d0acc99414e3..ef5fb2270960a9 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -518,14 +518,15 @@ desired effect in a number of ways. 1) By returning a tuple of the results:: - def func2(a, b): - a = 'new-value' # a and b are local names - b = b + 1 # assigned to new objects - return a, b # return new values - - x, y = 'old-value', 99 - x, y = func2(x, y) - print(x, y) # output: new-value 100 + >>> def func2(a, b): + ... a = 'new-value' # a and b are local names + ... b = b + 1 # assigned to new objects + ... return a, b # return new values + ... + >>> x, y = 'old-value', 99 + >>> x, y = func2(x, y) + >>> print(x, y) + new-value 100 This is almost always the clearest solution. @@ -533,38 +534,41 @@ desired effect in a number of ways. 3) By passing a mutable (changeable in-place) object:: - def func1(a): - a[0] = 'new-value' # 'a' references a mutable list - a[1] = a[1] + 1 # changes a shared object - - args = ['old-value', 99] - func1(args) - print(args[0], args[1]) # output: new-value 100 + >>> def func1(a): + ... a[0] = 'new-value' # 'a' references a mutable list + ... a[1] = a[1] + 1 # changes a shared object + ... + >>> args = ['old-value', 99] + >>> func1(args) + >>> print(args[0], args[1]) + new-value 100 4) By passing in a dictionary that gets mutated:: - def func3(args): - args['a'] = 'new-value' # args is a mutable dictionary - args['b'] = args['b'] + 1 # change it in-place - - args = {'a': 'old-value', 'b': 99} - func3(args) - print(args['a'], args['b']) # output: new-value 100 + >>> def func3(args): + ... args['a'] = 'new-value' # args is a mutable dictionary + ... args['b'] = args['b'] + 1 # change it in-place + ... + >>> args = {'a': 'old-value', 'b': 99} + >>> func3(args) + >>> print(args['a'], args['b']) + new-value 100 5) Or bundle up values in a class instance:: - class callByRef: - def __init__(self, **args): - for key, value in args.items(): - setattr(self, key, value) - - def func4(args): - args.a = 'new-value' # args is a mutable callByRef - args.b = args.b + 1 # change object in-place - - args = callByRef(a='old-value', b=99) - func4(args) - print(args.a, args.b) # output: new-value 100 + >>> class argSetter: + ... def __init__(self, /, **args): + ... for key, value in args.items(): + ... setattr(self, key, value) + ... + >>> def func4(args): + ... args.a = 'new-value' # args is a mutable argSetter + ... args.b = args.b + 1 # change object in-place + ... + >>> args = argSetter(a='old-value', b=99) + >>> func4(args) + >>> print(args.a, args.b) + new-value 100 There's almost never a good reason to get this complicated. From a7b67a98058e7a751ec9655c8aa092b4944b2326 Mon Sep 17 00:00:00 2001 From: Jiajie Zhong Date: Wed, 15 Jul 2020 23:34:29 +0800 Subject: [PATCH 3/7] Update Doc/faq/programming.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric Araujo --- Doc/faq/programming.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index ef5fb2270960a9..26aea30ca19c1d 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -524,9 +524,8 @@ desired effect in a number of ways. ... return a, b # return new values ... >>> x, y = 'old-value', 99 - >>> x, y = func2(x, y) - >>> print(x, y) - new-value 100 + >>> func2(x, y) + ('new-value', 100) This is almost always the clearest solution. From 66ea0ea7f27bf7f9a7ad940fb7d834919e78dc67 Mon Sep 17 00:00:00 2001 From: Jiajie Zhong Date: Wed, 15 Jul 2020 23:34:40 +0800 Subject: [PATCH 4/7] Update Doc/faq/programming.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric Araujo --- Doc/faq/programming.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 26aea30ca19c1d..415ffabdd9ffe8 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -539,8 +539,8 @@ desired effect in a number of ways. ... >>> args = ['old-value', 99] >>> func1(args) - >>> print(args[0], args[1]) - new-value 100 + >>> args + ['new-value', 100] 4) By passing in a dictionary that gets mutated:: From 51480b758515bfcef9020aa21c1678a6fd0c547f Mon Sep 17 00:00:00 2001 From: Jiajie Zhong Date: Wed, 15 Jul 2020 23:34:48 +0800 Subject: [PATCH 5/7] Update Doc/faq/programming.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric Araujo --- Doc/faq/programming.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 415ffabdd9ffe8..79ec23431eb2b0 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -550,8 +550,8 @@ desired effect in a number of ways. ... >>> args = {'a': 'old-value', 'b': 99} >>> func3(args) - >>> print(args['a'], args['b']) - new-value 100 + >>> args + {'a': 'new-value', 'b': 100} 5) Or bundle up values in a class instance:: From 99a9b4a0b07b8a04439ff3949e131cd43239b0b2 Mon Sep 17 00:00:00 2001 From: Jiajie Zhong Date: Wed, 15 Jul 2020 23:34:57 +0800 Subject: [PATCH 6/7] Update Doc/faq/programming.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Éric Araujo --- Doc/faq/programming.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 79ec23431eb2b0..150091f7394a5f 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -566,8 +566,8 @@ desired effect in a number of ways. ... >>> args = argSetter(a='old-value', b=99) >>> func4(args) - >>> print(args.a, args.b) - new-value 100 + >>> vars(args) + {'a': 'new-value', 'b': 100} There's almost never a good reason to get this complicated. From 5eec2771aa82b670bc009a20555d1801d9ce528c Mon Sep 17 00:00:00 2001 From: Jiajie Zhong Date: Wed, 15 Jul 2020 23:46:54 +0800 Subject: [PATCH 7/7] Change ex4 class name and switch function name --- Doc/faq/programming.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 150091f7394a5f..0731e92f6dbc60 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -518,13 +518,13 @@ desired effect in a number of ways. 1) By returning a tuple of the results:: - >>> def func2(a, b): + >>> def func1(a, b): ... a = 'new-value' # a and b are local names ... b = b + 1 # assigned to new objects ... return a, b # return new values ... >>> x, y = 'old-value', 99 - >>> func2(x, y) + >>> func1(x, y) ('new-value', 100) This is almost always the clearest solution. @@ -533,12 +533,12 @@ desired effect in a number of ways. 3) By passing a mutable (changeable in-place) object:: - >>> def func1(a): + >>> def func2(a): ... a[0] = 'new-value' # 'a' references a mutable list ... a[1] = a[1] + 1 # changes a shared object ... >>> args = ['old-value', 99] - >>> func1(args) + >>> func2(args) >>> args ['new-value', 100] @@ -555,16 +555,16 @@ desired effect in a number of ways. 5) Or bundle up values in a class instance:: - >>> class argSetter: + >>> class Namespace: ... def __init__(self, /, **args): ... for key, value in args.items(): ... setattr(self, key, value) ... >>> def func4(args): - ... args.a = 'new-value' # args is a mutable argSetter + ... args.a = 'new-value' # args is a mutable Namespace ... args.b = args.b + 1 # change object in-place ... - >>> args = argSetter(a='old-value', b=99) + >>> args = Namespace(a='old-value', b=99) >>> func4(args) >>> vars(args) {'a': 'new-value', 'b': 100}