Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
What is a Partial function
Like decorators, partial functions are used to extend functions. Unlike decorators, partial functions solve the problem of simplifying functions when the values of some parameters in a function are fixed in certain scenarios. What does that mean? Is to omit some invariable parameters, making the code more concise.
example
demand
Suppose we need to write a method called getInfo to get information about a human being. Roughly we need four parameters: country, city, name, and age.
The original code
# There are Chinese and Pakistanis
print('\ n = = = = = = = = = = = = = = = = = = = = here is Chinese & Pakistanis = = = = = = = = = = = = = = = = = = = =')
getInfo('China'.'xi 'an'.'fly'.18)
getInfo('Pakistan'.'Islamabad'.Sparta.19)
Copy the code
Chinese code
# It's all Chinese
print('\ n = = = = = = = = = = = = = = = = = = = = here are Chinese people = = = = = = = = = = = = = = = = = = = =')
getChinaInfo = partial(getInfo,country='China')
getChinaInfo(city='xi 'an',name='fly',age=18)
getChinaInfo(city='chengdu',name='yao',age=19)
Copy the code
Xi ‘an people’s code
When we only use this method to count Chinese people’s information, you will find that the value of the country parameter of getInfo is fixed to “China”. At this time, we need to use partial function to fix the country parameter (omitted), and create a new method getXianInfo.
# All from Xi 'an
print('\ n = = = = = = = = = = = = = = = = = = = = here is the person of xi 'an = = = = = = = = = = = = = = = = = = = =')
# When all the people are from Xi 'an, we don't need to spread to countries and cities anymore
You can use partial to fix parameters while creating new functions
getXianIifo = partial(getInfo,country='China',city='xi 'an')
getXianIifo(name='Ming',age=19)
getXianIifo(name='the bear',age=20)
Copy the code
Xi ‘an 18-year-old code
When we want to use this method to count the information of xi ‘an people, we also fixed the country and city with partial function and created the getXian18Info method.
# All 18-year-olds in Xi 'an
print('\ n = = = = = = = = = = = = = = = = = = the following are the age of 18 in xian = = = = = = = = = = = = = = = = = =')
getXian18Info = partial(getInfo,country='China',city='xi 'an',age=18)
getXian18Info(name='small beautiful')
getXian18Info(name='yellow')
Copy the code
Attach all code
from functools import partial
def getInfo(country,city,name,age) :
print(F 'I called{name}, I am{country}People, my hometown is{city}This year, I{age}Years old! ')
# There are Chinese and Pakistanis
print('\ n = = = = = = = = = = = = = = = = = = = = here is Chinese & Pakistanis = = = = = = = = = = = = = = = = = = = =')
getInfo('China'.'xi 'an'.'fly'.18)
getInfo('Pakistan'.'Islamabad'.Sparta.19)
# It's all Chinese
print('\ n = = = = = = = = = = = = = = = = = = = = here are Chinese people = = = = = = = = = = = = = = = = = = = =')
getChinaInfo = partial(getInfo,country='China')
getChinaInfo(city='xi 'an',name='fly',age=18)
getChinaInfo(city='chengdu',name='yao',age=19)
# All from Xi 'an
print('\ n = = = = = = = = = = = = = = = = = = = = here is the person of xi 'an = = = = = = = = = = = = = = = = = = = =')
# When all the people are from Xi 'an, we don't need to spread to countries and cities anymore
You can use partial to fix parameters while creating new functions
getXianIifo = partial(getInfo,country='China',city='xi 'an')
getXianIifo(name='Ming',age=19)
getXianIifo(name='the bear',age=20)
# All 18-year-olds in Xi 'an
print('\ n = = = = = = = = = = = = = = = = = = the following are the age of 18 in xian = = = = = = = = = = = = = = = = = =')
getXian18Info = partial(getInfo,country='China',city='xi 'an',age=18)
getXian18Info(name='small beautiful')
getXian18Info(name='yellow')
Copy the code
What about it? Did you quit school?
That’s all for today, thank you for reading, and we’ll see you next time.