1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-14 18:55:00 +02:00

[python/en] Fix @wraps decorator example (#5353)

The `__code__` example doesn't work as described but the `__doc__` does so I added a docstring and replaced the use of `__code__` with `__doc__`.
This commit is contained in:
Collin MacDonald
2025-08-08 14:05:04 -05:00
committed by GitHub
parent c10e273c6b
commit a21fbe4c6b

View File

@@ -1061,6 +1061,7 @@ def log_function(func):
@log_function # equivalent: @log_function # equivalent:
def my_function(x,y): # def my_function(x,y): def my_function(x,y): # def my_function(x,y):
"""Adds two numbers together."""
return x+y # return x+y return x+y # return x+y
# my_function = log_function(my_function) # my_function = log_function(my_function)
# The decorator @log_function tells us as we begin reading the function definition # The decorator @log_function tells us as we begin reading the function definition
@@ -1076,7 +1077,7 @@ my_function(1,2) # => "Entering function my_function"
# What happens if we try to get some information about my_function? # What happens if we try to get some information about my_function?
print(my_function.__name__) # => 'wrapper' 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. print(my_function.__doc__) # => None (wrapper function has no docstring)
# Because our decorator is equivalent to my_function = log_function(my_function) # Because our decorator is equivalent to my_function = log_function(my_function)
# we've replaced information about my_function with information from wrapper # we've replaced information about my_function with information from wrapper
@@ -1097,6 +1098,7 @@ def log_function(func):
@log_function @log_function
def my_function(x,y): def my_function(x,y):
"""Adds two numbers together."""
return x+y return x+y
my_function(1,2) # => "Entering function my_function" my_function(1,2) # => "Entering function my_function"
@@ -1104,7 +1106,7 @@ my_function(1,2) # => "Entering function my_function"
# => "Exiting function my_function" # => "Exiting function my_function"
print(my_function.__name__) # => 'my_function' print(my_function.__name__) # => 'my_function'
print(my_function.__code__.co_argcount) # => 2 print(my_function.__doc__) # => 'Adds two numbers together.'
``` ```
### Free Online ### Free Online