Partial Functions in Python — From Conceptual to Practical

Original article was published on Artificial Intelligence on Medium

Practical Usage Consideration

In the above section, we’ve seen several conceptual examples to understand what partial functions are and how to create partial functions. But you may be wondering what the practical use cases of partial functions are in our projects.

Well, it’s a tricky question because the purposes of our Python code can be very different. We all understand that Python is a general-purpose programming language. With so many open-source libraries available, Python has been widely used in web development, data science, and machine learning, just to name a few common use cases. Thus, a more relevant question is when we should consider using partial functions.

To answer this question, let’s briefly review the concept of partial functions. At its core, a partial function is to take advantage of an exiting function by providing some default arguments. What does it mean? With partial functions, we need to supply fewer arguments in order to call some exiting functions, which otherwise require more arguments. Thus, we can probably guess what best scenarios are for applying partial functions.

1. There are existing functions that serve general purposes.
2. Somehow, existing functions are a little clumsy to pass all needed arguments.
3. For some arguments, we don’t need to change them in particular settings and thus they can be set with fixed values.

If we can agree on these prerequisites, we can proceed to seeing some hypothetical, while potentially practical examples.

Example 1. Web Development
Let’s say that we’re building an online shoe store website. We have our products in the United States, and we sell and ship internationally. All the prices are saved as US dollars in the database. When users from different countries access our website, we want to display the prices in their respective local currencies.

Here’s some pseudo-code that takes advantage of partial functions.

# Functions that are for general purposes
def get_price_local_currency(conversion_rate, us_price):
local_price = conversion_rate * us_price
return local_price
def get_current_country_code():
# get the current user's country code
country_code = "UK"
return country_code
def get_conversion_rate(country_code):
# get the currency conversion rate somewhere
conversion_rate = 0.8
return conversion_rate
# Chains of things to happen after a user in UK accesses the websitecountry_code = get_current_country_code()
conversion_rate = get_conversion_rate(country_code)
get_price = lambda x: get_price_local_currency(conversion_rate, x)
# For various places/products, we can now simply use the get_price function to get/show the prices in the local currencyproduct0_price = get_price(100.95)
product1_price = get_price(80)
...

Example 2. Data Science
Python is a popular choice as the go-to programming language in data science. I use it a lot in my scientific research for data analysis purposes too. The possible specific use case can be summarized here.

Through years of research, we have already built various statistical models, such as logistic regression and mixed models. For different datasets, we need to apply pertinent models.

The following pseudo-code can address this need. Again, three essential components are involved. 1). Have general functions to begin with; 2). Create partial functions for specific needs; 3) Apply the partial functions.

# Functions that are for general purposes
def run_stats_model(model, output_path, dataset):
# do something with the dataset if applicable
# run the model using the dataset
# save the stats outcome to the output_path
return success_code if succeeded else failure_code
# When we're running some analysis with multiple datasets for my colleague Dr. Smith involving Model XYZ
run_model_XYZ = lambda x: run_stats_model(XYZ, "X:/Smith/Stats", x)
# Run the models with various datasets
status_code0 = run_model_XYZ(dataset0)
status_code1 = run_model_XYZ(dataset1)
...