Python functional programming, just read this article!

This article provides a simple introduction to functional programming technology in Python.

First-class functions
In Python, functions are "first-class citizens". That is, functions are on an equal footing with other data types such as int.
Thus, we can assign functions to variables, pass them as arguments to other functions, store them in other data structures (such as dicts), and use them as return values of other functions.
Treat functions as objects
Since other data types (such as string, list, and int) are objects, then functions are also objects in Python. Let's look at the example function foo, which prints its own name:
def foo():
print("foo")Since functions are objects, we can assign function foo to any variable and then call that variable. For example, we can assign a function to the variable bar:
bar = foo bar() #will print "foo" to the console
The statement bar = foo assigns the object referenced by the function foo to the variable bar.
Treating objects as functions
When objects are callable, they are the same as functions, such as object(). This is achieved through the __call__ method.
The example is as follows:
class Greeter: def __init__(self, greeting): self.greeting = greeting def __call__(self, name): return self.greeting + " " + name
Every time we configure an object of the Greeter class, we will create a new object, which is a new name that can be shouted when greeting. As shown below:
morning = Greeter("good morning") #creates the callable object
morning("john") # calling the object
#prints "good morning john" to the consoleThe reason why we can call the morning object is that we have used the __call__ method in the class definition. To check if an object is callable, we use the built-in function callable:
callable(morning) #true callable(145) #false. int is not callable.
Functions inside data structures
Functions, like other objects, can be stored inside data structures. For example, we can create a dictionary of int to func. This comes in handy when an int is a shorthand for the step to be performed.
# store in dictionary
mapping = {
0 : foo,
1 : bar
}
x = input() #get integer value from user
mapping[x]() #call the func returned by dictionary accessSimilarly, functions can be stored in a variety of other data structures.
Use functions as parameters and return values
Functions can also be used as parameters and return values of other functions. Functions that accept functions as input or return functions are called higher-order functions, and they are an important part of functional programming.
Higher-order functions have powerful capabilities. As explained in "Eloquent JavaScript":
- "Higher-order functions allow us to abstract actions, not just abstract values."
Let's take a look one example. Suppose we want to iterate over a list of items and print them out in sequence. We can easily build an iterate function:
def iterate(list_of_items): for item in list_of_items: print(item)
It looks cool, but it’s just a first-level abstraction. What if we want to do something other than print while iterating over the list?
This is the meaning of the existence of higher-order functions. We can create a function iterate_custom, where the list to be iterated over and the function to be applied to each item are the inputs to the iterate_custom function:
def iterate_custom(list_of_items, custom_func): for item in list_of_items: custom_func(item)
This may seem trivial, but it is actually very powerful.
We have raised the level of abstraction to make the code more reusable. Now, not only can we call this function when printing a list, we can also perform arbitrary operations on the list involving sequence iteration.
Functions can also be returned, making things even simpler. Just like we store functions in dict, we can also use functions as control statements to determine the appropriate function. For example:
def add(x, y): return x + y def sub(x, y): return x - y def mult(x, y): return x * y def calculator(opcode): if opcode == 1: return add elif opcode == 2: return sub else: return mult my_calc = calculator(2) #my calc is a subtractor my_calc(5, 4) #returns 5 - 4 = 1 my_calc = calculator(9) #my calc is now a multiplier my_calc(5, 4) #returns 5 x 4 = 20.
Nested functions
Functions can also be inside other functions. This is an "internal function". Intrinsic functions are useful when creating helper functions, small reusable functions that serve as submodules to support the main function.
We can use auxiliary functions when the problem requires a specific function definition (parameter type or order). This non-traditional approach makes problem solving much simpler, see for example: http://www-inst.eecs.berkeley.edu/~cs61a/sp12/lectures/lect4-2x3.pdf.
Suppose you want to define a Fibonacci function fib(n), which has only one parameter n, and we must return the nth Fibonacci number.
One possible way to define such a function is to use a helper function to track the first two terms of the Fibonacci sequence (since the Fibonacci number is the sum of the first two numbers).
def fib(n): def fib_helper(fk1, fk, k): if n == k: return fk else: return fib_helper(fk, fk1+fk, k+1) if n <= 1: return n else: return fib_helper(0, 1, 1)
Move the calculation from the function body to the function parameters, which is very powerful. Because it reduces redundant calculations that may occur in recursive methods.
Single expression function (Lambda expression)
What should we do if we want to write a function before giving the function a name? What if we want to write a short one-line function (such as function foo or mult in the example above)?
We can use the lambda keyword in Python to define such functions. An example is as follows:
mult = lambda x, y: x * y mult(1, 2) #returns 2
The mult function behaves the same as a function defined using the traditional def keyword.
Note: The lambda function must be a single line and cannot contain return statements written by the programmer.
事实上,它们通常具备隐式的返回语句(在上面的示例中,函数想表达 return x * y,不过我们省略了 lambda 函数中的显式返回语句)。
lambda 函数更加强大和精准,因为我们还可以构建匿名函数(即没有名称的函数):
(lambda x, y: x * y)(9, 10) #returns 90
当我们只需要一次性使用某函数时,这种方法非常方便。例如,当我们想填充字典时:
import collections pre_fill = collections.defaultdict(lambda: (0, 0)) #all dictionary keys and values are set to 0
接下来我们来看 Map、Filter 和 Reduce,以更多地了解 lambda。
Map、Filter 和 Reduce
Map
map 函数基于指定过程(函数)将输入集转换为另一个集合。这类似于上文提到的 iterate_custom 函数。例如:
def multiply_by_four(x): return x * 4 scores = [3, 6, 8, 3, 5, 7] modified_scores = list(map(multiply_by_four, scores)) #modified scores is now [12, 24, 32, 12, 20, 28]
在 Python 3 中,map 函数返回的 map 对象可被类型转换为 list,以方便使用。现在,我们无需显式地定义 multiply_by_four 函数,而是定义 lambda 表达式:
modified_scores = list(map(lambda x: 4 * x, scores))
当我们想对集合内的所有值执行某项操作时,map 函数很有用。
Filter
就像名称所显示的那样,filter 函数可以帮助筛除不想要的项。例如,我们想要去除 scores 中的奇数,那么我们可以使用 filter:
even_scores = list(filter(lambda x: True if (x % 2 == 0) else False, scores)) #even_scores = [6, 8]
由于提供给 filter 的函数是逐个决定是否接受每一个项的,因此该函数必须返回 bool 值,且该函数必须是一元函数(即只使用一个输入参数)。
Reduce
reduce 函数用于「总结」或「概述」数据集。例如,如果我们想要计算所有分数的总和,就可以使用 reduce:
sum_scores = reduce((lambda x, y: x + y), scores) #sum_scores = 32
这要比写循环语句简单多了。注意:提供给 reduce 的函数需要两个参数:一个表示正在接受检查的项,另一个表示所用运算的累积结果。
本文是关于函数式编程的一篇入门文章,虽然尽量完备地介绍了相关的知识,但并不是那么深入。如想了解更多,大家可以阅读以下资源:
- Best Practices for Using Functional Programming in Python:https://kite.com/blog/python/functional-programming/
- Functional Programming Tutorials and Notes:https://www.hackerearth.com/zh/practice/python/functional-programming/functional-programming-1/tutorial/
The above is the detailed content of Python functional programming, just read this article!. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
What are class methods in Python
Aug 21, 2025 am 04:12 AM
ClassmethodsinPythonareboundtotheclassandnottoinstances,allowingthemtobecalledwithoutcreatinganobject.1.Theyaredefinedusingthe@classmethoddecoratorandtakeclsasthefirstparameter,referringtotheclassitself.2.Theycanaccessclassvariablesandarecommonlyused
python asyncio queue example
Aug 21, 2025 am 02:13 AM
asyncio.Queue is a queue tool for secure communication between asynchronous tasks. 1. The producer adds data through awaitqueue.put(item), and the consumer uses awaitqueue.get() to obtain data; 2. For each item you process, you need to call queue.task_done() to wait for queue.join() to complete all tasks; 3. Use None as the end signal to notify the consumer to stop; 4. When multiple consumers, multiple end signals need to be sent or all tasks have been processed before canceling the task; 5. The queue supports setting maxsize limit capacity, put and get operations automatically suspend and do not block the event loop, and the program finally passes Canc
How to run a Python script and see the output in a separate panel in Sublime Text?
Aug 17, 2025 am 06:06 AM
ToseePythonoutputinaseparatepanelinSublimeText,usethebuilt-inbuildsystembysavingyourfilewitha.pyextensionandpressingCtrl B(orCmd B).2.EnsurethecorrectbuildsystemisselectedbygoingtoTools→BuildSystem→Pythonandconfirming"Python"ischecked.3.Ifn
How to use regular expressions with the re module in Python?
Aug 22, 2025 am 07:07 AM
Regular expressions are implemented in Python through the re module for searching, matching and manipulating strings. 1. Use re.search() to find the first match in the entire string, re.match() only matches at the beginning of the string; 2. Use brackets() to capture the matching subgroups, which can be named to improve readability; 3. re.findall() returns all non-overlapping matches, and re.finditer() returns the iterator of the matching object; 4. re.sub() replaces the matching text and supports dynamic function replacement; 5. Common patterns include \d, \w, \s, etc., you can use re.IGNORECASE, re.MULTILINE, re.DOTALL, re
How to build and run Python in Sublime Text?
Aug 22, 2025 pm 03:37 PM
EnsurePythonisinstalledbyrunningpython--versionorpython3--versionintheterminal;ifnotinstalled,downloadfrompython.organdaddtoPATH.2.InSublimeText,gotoTools>BuildSystem>NewBuildSystem,replacecontentwith{"cmd":["python","-
How to use variables and data types in Python
Aug 20, 2025 am 02:07 AM
VariablesinPythonarecreatedbyassigningavalueusingthe=operator,anddatatypessuchasint,float,str,bool,andNoneTypedefinethekindofdatabeingstored,withPythonbeingdynamicallytypedsotypecheckingoccursatruntimeusingtype(),andwhilevariablescanbereassignedtodif
How to pass command-line arguments to a script in Python
Aug 20, 2025 pm 01:50 PM
Usesys.argvforsimpleargumentaccess,whereargumentsaremanuallyhandledandnoautomaticvalidationorhelpisprovided.2.Useargparseforrobustinterfaces,asitsupportsautomatichelp,typechecking,optionalarguments,anddefaultvalues.3.argparseisrecommendedforcomplexsc
How to debug a remote Python application in VSCode
Aug 30, 2025 am 06:17 AM
To debug a remote Python application, you need to use debugpy and configure port forwarding and path mapping: First, install debugpy on the remote machine and modify the code to listen to port 5678, forward the remote port to the local area through the SSH tunnel, then configure "AttachtoRemotePython" in VSCode's launch.json and correctly set the localRoot and remoteRoot path mappings. Finally, start the application and connect to the debugger to realize remote breakpoint debugging, variable checking and code stepping. The entire process depends on debugpy, secure port forwarding and precise path matching.


