In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "python functional programming example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "python functional programming example analysis" bar!
Introduction
Python has a lot of useful functions built in, which we can call directly.
To call a function, you need to know the name and parameters of the function, such as the function abs that seeks the absolute value, which has only one argument.
You can also view the help information for the abs function through help (abs) on the interactive command line.
Call the abs function:
> abs (100,100) > abs (- 20) 20 > abs (12.34) 12.34 functional programming
A function in Python is a piece of code that is designed to implement a function and can be reused.
That is, do not repeat the wheel in the future, and use that function when you encounter that scene, that is, functional programming.
Next, I define a my_func, pass in a Hello World, and print a Hello World
Def my_func (message): print ('Got a message: {}' .format (message)) # call function my_func () my_func ('Hello World') # output Got a message: Hello World
Simple knowledge points
Def is a declaration of a function
My_func is the name of the function
Message is an argument to a function
Print is the body of the function
The result of the call (return or yield) can be returned at the end of the function or not.
"define before, call after"
Def my_sum (a, b): return a + bresult = my_sum (3,5) print (result) # output 8
You can set default values for the parameters of the function
Def func (param = 0): pass
If param is not passed in, the parameter defaults to 0. If the parameter is passed in, the default value is overridden.
Polymorphisms
The parameters passed in can accept any data type
For example, a list
Print (my_sum ([1,2], [3,4])) # output [1,2,3,4]
For example, a string
Print (my_sum ('hello', 'world')) # output hello world
Of course, if the parameter data types are different and the two cannot be added together
Print (my_sum ([1,2], 'hello')) TypeError: can only concatenate list (not "str") to list
Operations where the same function can be applied to integers, lists, strings, and so on are called polymorphisms. This is not a pervert.
Nested function
Function nesting is a function in a function, which is called a nested function.
Def F1 (): print ('hello') def f2 (): print (' world') f2 () F1 () # output helloworld
The nesting of functions ensures the call of internal functions, which can only be called by external functions and will not act in the global domain.
Rational use of function nesting to improve operation speed
For example, calculate the factorial of 5.
Def factorial (input): if not isinstance (input, int): raise Exception ('input must be an integer.') If input < 0: raise Exception ('input must be greater or equal to 0') def inner_factorial (input): if input MAX_VALUE: raise Exception (' validation check fails')
MIN_VELUE and MAX_VALUE here are global variables, but we can't change the value of global variables at will inside the function.
MIN_VALUE = 1def validation_check (value): MIN_VALUE + = 1 validation_check (5) error: UnboundLocalError: local variable 'MIN_VALUE' referenced before assignment
If you want to change, you must add the statement of global.
MIN_VALUE = 1def validation_check (value): global MIN_VALUE MIN_VALUE + = 1 validation_check (5)
Global tells the python parser that the variable MIN_VALUE inside the function is the defined global variable, where the input is 2, so the value of the global variable is modified.
MIN_VALUE = 1MAX_VALUE = 10def validation_check (): MIN_VALUE = 3 print (MIN_VALUE) validation_check () print (MIN_VALUE) # nobody 1
For nested functions, internal functions cannot modify variables defined by external functions and can be accessed. If you want to modify them, add nonolocal.
Def outer (): X = "local" def inner (): nonlocal x # nonlocal keyword means that x here is the variable defined by the external function outer x = 'nonlocal' print ("inner:", x) inner () print ("outer:", x) outer () # output inner: nonlocalouter: nonlocal
If you don't add it, you won't cover it.
Def outer (): X = "local" def inner (): X = 'nonlocal' # where x is the local variable print ("inner:", x) inner () print ("outer:", x) outer () # output inner: nonlocalouter: local closure of the function inner
The closure of a function is actually very similar to the nesting of a function. Unlike nesting, the outer function of a closure returns a function, not a specific value.
Closure is to call a function in a function, which is usually executed by return, and return gives the name of the function called internally.
Let's next calculate the nth power of the next number, using a closure to write as follows:
Def nth_power (exponent): def exponent_of (base): return base * * exponent return exponent_of # the return value is the exponent_of function square = nth_power (2) # calculate the cube of a number = nth_power (3) # calculate the cubic square# output of a number print (square (2)) # the square cube# of calculation 2 (cube (2)) # the cube of calculation 2 # output 4 # 2 ^ 28 # 2 ^ 3
Of course, we can also write this function through a function:
Def nth_power (base,exponent): return base**exponent
However, using closures can make the program more concise and easy to understand.
At this point, I believe that everyone on the "python functional programming example analysis" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.