From 73584a2e276cd52214f73443eda689c773973691 Mon Sep 17 00:00:00 2001 From: "Stanislav (Stanley) Modrak" <44023416+smith558@users.noreply.github.com> Date: Fri, 25 Aug 2023 04:52:13 +0100 Subject: Clarify "Method Resolution Order" (#4687) --- python.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index d9eda60c..d9261ff2 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -876,7 +876,8 @@ if __name__ == '__main__': if type(sup) is Superhero: print('I am a superhero') - # Get the Method Resolution search Order used by both getattr() and super() + # Get the "Method Resolution Order" used by both getattr() and super() + # (the order in which classes are searched for an attribute or method) # This attribute is dynamic and can be updated print(Superhero.__mro__) # => (, # => , ) @@ -958,8 +959,7 @@ class Batman(Superhero, Bat): if __name__ == '__main__': sup = Batman() - # Get the Method Resolution search Order used by both getattr() and super(). - # This attribute is dynamic and can be updated + # The Method Resolution Order print(Batman.__mro__) # => (, # => , # => , -- cgit v1.2.3 From 7b2491ecd5b3370b7cc712fced32b948440ecb40 Mon Sep 17 00:00:00 2001 From: triumphantomato <91909240+triumphantomato@users.noreply.github.com> Date: Thu, 7 Sep 2023 22:29:58 -0700 Subject: [python/en] Updated Decorator and wrapping explanation (#4749) Now it includes motivation, an explanation of functools.wraps, and demonstrates the utility of wrapping. --- python.html.markdown | 70 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 17 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index d9261ff2..91a53360 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -1016,31 +1016,67 @@ gen_to_list = list(values) print(gen_to_list) # => [-1, -2, -3, -4, -5] -# Decorators -# In this example `beg` wraps `say`. If say_please is True then it -# will change the returned message. -from functools import wraps +# Decorators are a form of syntactic sugar. +# They make code easier to read while accomplishing clunky syntax. +# Wrappers are one type of decorator. +# They're really useful for adding logging to existing functions without needing to modify them. -def beg(target_function): - @wraps(target_function) +def log_function(func): def wrapper(*args, **kwargs): - msg, say_please = target_function(*args, **kwargs) - if say_please: - return "{} {}".format(msg, "Please! I am poor :(") - return msg - + print("Entering function", func.__name__) + result = func(*args, **kwargs) + print("Exiting function", func.__name__) + return result return wrapper +@log_function # equivalent: +def my_function(x,y): # def my_function(x,y): + return x+y # return x+y + # my_function = log_function(my_function) +# The decorator @log_function tells us as we begin reading the function definition +# for my_function that this function will be wrapped with log_function. +# When function definitions are long, it can be hard to parse the non-decorated +# assignment at the end of the definition. + +my_function(1,2) # => "Entering function my_function" + # => "3" + # => "Exiting function my_function" + +# But there's a problem. +# What happens if we try to get some information about my_function? + +print(my_function.__name__) # => 'wrapper' +print(my_function.__code__.co_argcount) # => 0. The argcount is 0 because both arguments in wrapper()'s signature are optional. + +# Because our decorator is equivalent to my_function = log_function(my_function) +# we've replaced information about my_function with information from wrapper + +# Fix this using functools + +from functools import wraps + +def log_function(func): + @wraps(func) # this ensures docstring, function name, arguments list, etc. are all copied + # to the wrapped function - instead of being replaced with wrapper's info + def wrapper(*args, **kwargs): + print("Entering function", func.__name__) + result = func(*args, **kwargs) + print("Exiting function", func.__name__) + return result + return wrapper -@beg -def say(say_please=False): - msg = "Can you buy me a beer?" - return msg, say_please +@log_function +def my_function(x,y): + return x+y + +my_function(1,2) # => "Entering function my_function" + # => "3" + # => "Exiting function my_function" +print(my_function.__name__) # => 'my_function' +print(my_function.__code__.co_argcount) # => 2 -print(say()) # Can you buy me a beer? -print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :( ``` ### Free Online -- cgit v1.2.3 From e9f263a75f0ec9c2984307d0fd4503f8476c02fa Mon Sep 17 00:00:00 2001 From: triumphantomato <91909240+triumphantomato@users.noreply.github.com> Date: Thu, 7 Sep 2023 22:30:31 -0700 Subject: [python/en] add hint about leading underscore indicating internal use (#4748) --- python.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 91a53360..efeb0e86 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -733,7 +733,9 @@ class Human: self.name = name # Initialize property - self._age = 0 + self._age = 0 # the leading underscore indicates the "age" property is + # intended to be used internally + # do not rely on this to be enforced: it's a hint to other devs # An instance method. All methods take "self" as the first argument def say(self, msg): -- cgit v1.2.3 From 4c6be14b8acf5f0f3495d06249406edc5dde72f2 Mon Sep 17 00:00:00 2001 From: triumphantomato <91909240+triumphantomato@users.noreply.github.com> Date: Thu, 7 Sep 2023 22:31:15 -0700 Subject: Update expected results for ceil and floor (#4747) ceil and floor return ints not floats - check in repl or the python3 docs here: https://docs.python.org/3/library/math.html --- python.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index efeb0e86..326ddb95 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -685,8 +685,8 @@ print(math.sqrt(16)) # => 4.0 # You can get specific functions from a module from math import ceil, floor -print(ceil(3.7)) # => 4.0 -print(floor(3.7)) # => 3.0 +print(ceil(3.7)) # => 4 +print(floor(3.7)) # => 3 # You can import all functions from a module. # Warning: this is not recommended -- cgit v1.2.3 From 38bf258ad82363e8a8eb06064b223aa2354b0186 Mon Sep 17 00:00:00 2001 From: "Stanislav (Stanley) Modrak" <44023416+smith558@users.noreply.github.com> Date: Wed, 20 Sep 2023 12:51:11 +0100 Subject: Fix incorrect expression description --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 326ddb95..c9eac8ae 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -226,7 +226,7 @@ li[4] # Raises an IndexError li[1:3] # Return list from index 1 to 3 => [2, 4] li[2:] # Return list starting from index 2 => [4, 3] li[:3] # Return list from beginning until index 3 => [1, 2, 4] -li[::2] # Return list selecting every second entry => [1, 4] +li[::2] # Return list selecting elements with a step size of 2 => [1, 4] li[::-1] # Return list in reverse order => [3, 4, 2, 1] # Use any combination of these to make advanced slices # li[start:end:step] -- cgit v1.2.3 From 0a0b3555d5e9923907bb2b8e953a11c5b94a1464 Mon Sep 17 00:00:00 2001 From: "Stanislav (Stanley) Modrak" <44023416+smith558@users.noreply.github.com> Date: Thu, 21 Sep 2023 17:26:42 +0100 Subject: Clarify args and kwargs --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index c9eac8ae..75b1ec85 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -603,7 +603,7 @@ all_the_args(1, 2, a=3, b=4) prints: """ # When calling functions, you can do the opposite of args/kwargs! -# Use * to expand tuples and use ** to expand kwargs. +# Use * to expand args (tuples) and use ** to expand kwargs (dictionaries). args = (1, 2, 3, 4) kwargs = {"a": 3, "b": 4} all_the_args(*args) # equivalent: all_the_args(1, 2, 3, 4) -- cgit v1.2.3 From 9e87e133a35ba1033e2627ff0e2ad3f0f482ca0f Mon Sep 17 00:00:00 2001 From: Carlos Tafur Date: Thu, 14 Dec 2023 16:23:10 +0100 Subject: Update python.html.markdown (#4228) I read somewhere that conventions in naming variables are snake_case and camelCase. --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 75b1ec85..49b46711 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -185,7 +185,7 @@ print("Hello, World", end="!") # => Hello, World! input_string_var = input("Enter some data: ") # Returns the data as a string # There are no declarations, only assignments. -# Convention is to use lower_case_with_underscores +# Convention in naming variables is snake_case style some_var = 5 some_var # => 5 -- cgit v1.2.3 From 05aa44a0419b735dcb6d5e63347c1e0d4ba9fc8e Mon Sep 17 00:00:00 2001 From: Mohammed Ashour Date: Thu, 14 Dec 2023 19:58:53 +0100 Subject: =?UTF-8?q?[python/en]=20adding=20an=20example=20for=20closures=20?= =?UTF-8?q?in=20python=20where=20we=20use=20the=20`nonlocal`=20=E2=80=A6?= =?UTF-8?q?=20(#4033)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * adding an example for closures in python where we use the `nonlocal` keyword Adding an example for the case of nested functions with closure scopes, when we use the python 3s keyword 'nonlocal' to point to the variables in the outer functions * Formatting Comments --- python.html.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 49b46711..967c2bd7 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -655,6 +655,22 @@ def create_adder(x): add_10 = create_adder(10) add_10(3) # => 13 +# Closures in nested functions: +# We can use the nonlocal keyword to work with variables in nested scope which shouldn't be declared in the inner functions. +def create_avg(): + total = 0 + count = 0 + def avg(n): + nonlocal total, count + total += n + count += 1 + return total/count + return avg +avg = create_avg() +avg(3) # => 3.0 +avg(5) # (3+5)/2 => 4.0 +avg(7) # (8+7)/2 => 5.0 + # There are also anonymous functions (lambda x: x > 2)(3) # => True (lambda x, y: x ** 2 + y ** 2)(2, 1) # => 5 -- cgit v1.2.3 From e268272771f19189c758280b5b27aad39c94fef1 Mon Sep 17 00:00:00 2001 From: bkkavin <70782359+bkkavin@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:00:36 +0530 Subject: typo in learn x in y minutes python page (#4812) --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python.html.markdown') diff --git a/python.html.markdown b/python.html.markdown index 967c2bd7..d863bcc3 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -669,7 +669,7 @@ def create_avg(): avg = create_avg() avg(3) # => 3.0 avg(5) # (3+5)/2 => 4.0 -avg(7) # (8+7)/2 => 5.0 +avg(7) # (8+7)/3 => 5.0 # There are also anonymous functions (lambda x: x > 2)(3) # => True -- cgit v1.2.3