Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of python partial function

Shulou Source: shulou.com Published: 2022-06-03 00:47:46 09月10日 Update

This article is to share with you about the use of python partial functions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. The partial function is the secondary encapsulation of the original function, which binds some parameters of the existing function to the specified value in advance to obtain a new function.

2. To define a partial function, you need to use the partial keyword (located in the functools module).

Grammar

Partial function name = partial (func, * args, * * kwargs)

Example

From functools import partial # imports the partial function in the functools module # defines an original function def display (name,age): print ("name:", name, "age:", age) # defines a partial function, which encapsulates the display () function and sets the default parameter GaryFun = partial (display,name = 'Gary') for the name parameter. Because the name parameter already has a default value, you can call the partial function without specifying GaryFun (age = 13). Thank you for reading! This is the end of this article on "what is the use of python partial function". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Functions parameters content more modules articles encapsulation good original practical key keywords examples articles see knowledge grammar parts references help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi Shulou Technology vpn OPPO Reno