From 9218c1fcfb0eaf666c8616b3031e8ec751af86fc Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 14 Aug 2025 10:57:19 +0400 Subject: [PATCH 1/3] Added partial.md in functools module --- .../functools-module/terms/partial/partial.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 content/python/concepts/functools-module/terms/partial/partial.md diff --git a/content/python/concepts/functools-module/terms/partial/partial.md b/content/python/concepts/functools-module/terms/partial/partial.md new file mode 100644 index 00000000000..b925d667290 --- /dev/null +++ b/content/python/concepts/functools-module/terms/partial/partial.md @@ -0,0 +1,60 @@ +--- +Title: functools-partial() +Description: functools.partial() is a function in the Python functools module that allows you to "pre-fill" or "fix" some arguments of a function, returning a new function with fewer arguments. This is useful for creating specialized or simplified versions of existing functions. + +Subjects: + - Computer Science + +Tags: + - Python + - Functions + +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' + +--- + +`functools.partial()` is a function provided by Python’s `functools` module that allows you to **fix (pre-fill) some arguments** of a function and return a new function with fewer arguments. + + +## Syntax + +python +from functools import partial + +partial_func = partial(func, /, *args, **keywords) + +## Example + +from functools import partial + +def power(base, exponent): + return base ** exponent + +# Create a version of power that always squares the input +square = partial(power, exponent=2) + +print(square(5)) # Output: 25 + +## Codebyte Example (if applicable) + +We can currently support: + +- Python + +from functools import partial + +def greet(greeting, name): + return f"{greeting}, {name}!" + +# Create a new function that always uses "Hello" as the greeting +say_hello = partial(greet, "Hello") + +# Use the partial function +print(say_hello("Alice")) # Output: Hello, Alice! +print(say_hello("Bob")) # Output: Hello, Bob! +See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! + +```codebyte/js + From 9f4ae8a5ef680f6ec016836bb65e2305aaeb724a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 15 Aug 2025 15:46:10 +0530 Subject: [PATCH 2/3] content changes --- .../functools-module/terms/partial/partial.md | 69 +++++++++++-------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/content/python/concepts/functools-module/terms/partial/partial.md b/content/python/concepts/functools-module/terms/partial/partial.md index b925d667290..55417aabe5c 100644 --- a/content/python/concepts/functools-module/terms/partial/partial.md +++ b/content/python/concepts/functools-module/terms/partial/partial.md @@ -1,60 +1,69 @@ --- -Title: functools-partial() -Description: functools.partial() is a function in the Python functools module that allows you to "pre-fill" or "fix" some arguments of a function, returning a new function with fewer arguments. This is useful for creating specialized or simplified versions of existing functions. - -Subjects: - - Computer Science - +Title: 'partial()' +Description: ''Creates a new function with some arguments of the original function pre-filled.' +Subjects: + - Computer Science' + - 'Data Science' Tags: - - Python - - Functions - + - 'Functions' + - 'Python' CatalogContent: - 'learn-python-3' - 'paths/computer-science' - --- -`functools.partial()` is a function provided by Python’s `functools` module that allows you to **fix (pre-fill) some arguments** of a function and return a new function with fewer arguments. - +The **`functools.partial()`** function in Python’s [`functools`](https://docs.python.org/3/library/functools.html) module returns a new callable with some arguments of the original function fixed (pre-filled). This can simplify calls by creating specialized versions of existing functions. ## Syntax -python +```pseudo from functools import partial - partial_func = partial(func, /, *args, **keywords) +``` + +**Parameters:** -## Example +- `func`: The original callable to be partially applied. +- `*args`: Positional arguments to pre-fill. +- `**keywords`: Keyword arguments to pre-fill. +**Return value:** + +A `functools.partial` object that behaves like the original function but with the specified arguments fixed. + +## Example: Pre-filling a Keyword Argument + +In this example, the `exponent` parameter is fixed to 2, creating a specialized version of the `power` function that always squares the input: + +```py from functools import partial def power(base, exponent): - return base ** exponent + return base ** exponent -# Create a version of power that always squares the input square = partial(power, exponent=2) -print(square(5)) # Output: 25 +print(square(5)) +``` -## Codebyte Example (if applicable) +The output of this code is: -We can currently support: +```shell +25 +``` -- Python +## Codebyte Example: Pre-filling a Positional Argument +This example fixes the `greeting` argument of the `greet` function to always use `"Hello"`: + +```codebyte/python from functools import partial def greet(greeting, name): - return f"{greeting}, {name}!" + return f"{greeting}, {name}!" -# Create a new function that always uses "Hello" as the greeting say_hello = partial(greet, "Hello") -# Use the partial function -print(say_hello("Alice")) # Output: Hello, Alice! -print(say_hello("Bob")) # Output: Hello, Bob! -See [content-standards.md](https://github.com/Codecademy/docs/blob/main/documentation/content-standards.md) for more details! - -```codebyte/js - +print(say_hello("Alice")) +print(say_hello("Bob")) +``` From 68b19d4e48a8f2773776a59f11b01348977ce731 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 15 Aug 2025 15:47:13 +0530 Subject: [PATCH 3/3] Update partial.md --- .../python/concepts/functools-module/terms/partial/partial.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/python/concepts/functools-module/terms/partial/partial.md b/content/python/concepts/functools-module/terms/partial/partial.md index 55417aabe5c..735fa909f32 100644 --- a/content/python/concepts/functools-module/terms/partial/partial.md +++ b/content/python/concepts/functools-module/terms/partial/partial.md @@ -1,8 +1,8 @@ --- Title: 'partial()' -Description: ''Creates a new function with some arguments of the original function pre-filled.' +Description: 'Creates a new function with some arguments of the original function pre-filled.' Subjects: - - Computer Science' + - 'Computer Science' - 'Data Science' Tags: - 'Functions'