1. Snowball learning started in the fourth quarter of Python. One needs three meals. I'm familiar with Python functional programming, process-oriented, object-oriented and functional

InfoQ 2021-10-29 19:43:40
snowball learning started fourth quarter

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Snowballing Python The fourth round , This time we have to learn something difficult , therefore , Erasers will reduce the difficulty of reading and understanding , Try to use vernacular to pave the way for you .","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Write it at the front ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" This round of learning , Very biased theory , Because some concepts involved are also the style of other programming languages , And there are some disputes in the actual landing ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" But learn more , There is always no harm .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Snowballing Python The fourth round , Mainly learn functional programming ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Each article in this series 3000 Word left or right ( Include code ), So rest assured to enjoy , It will not increase the daily learning intensity ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Snowball History Series , Completed 3 A column , Updating 1 A column , That is, in the third round of learning update , So far 21 piece ~, Since the third round is project practice , learn Django Go to , So the fourth round of concept classes starts synchronously .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/hihell/category_10573584.html","title":"","type":null},"content":[{"type":"text","text":" Snowballing Python( The end )","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/hihell/category_10763952.html","title":"","type":null},"content":[{"type":"text","text":" Snowballing Python The second round ( The end )","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/hihell/category_10901284.html","title":"","type":null},"content":[{"type":"text","text":" Snowballing Python The third round ( Updating )","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://blog.csdn.net/hihell/category_10656235.html","title":"","type":null},"content":[{"type":"text","text":" Snowballing Python Set pieces ( The end )","attrs":{}}]}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Python Functional programming ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Python Not a purely functional language , But you can use Python Do functional programming ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Typical listening to you , If you listen to me , To put it bluntly Python With the characteristics of functional programming ,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"so, You can borrow the design pattern and programming technology of functional language , Write code like functional programming ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" I usually boast at this time , Functional code comparison ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" Simplicity and elegance ","attrs":{}},{"type":"text","text":"~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Okay , It's all boasted .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The above contents belong to the scope of reasoning , That's in Python What are the skill points suitable for functional programming ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" What are the discomfort points ?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Following 2 Just make an impression ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" advantage : Generator Expressions , This will be mentioned again and again later , With many higher-order functions , for example ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"reduce","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"map","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"filter","attrs":{}}],"attrs":{}},{"type":"text","text":" Three giants .","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" shortcoming : No infinite recursion, etc ~","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" If you go to Baidu “ What is functional programming ”, Many places will give answers ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Functional programming : Allows the function itself to be passed as an argument to another function , It also allows you to return a function .","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" with reason !","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" In fact, functional programming is ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" Define the expression in the function and the method to implement the expression ","attrs":{}},{"type":"text","text":", To put it bluntly, it is to use functions to land your code .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" It looks like nonsense , It also has a supplementary explanation , In functional programming, avoid state changes and use variable objects .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" among ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" Avoid state changes ","attrs":{}},{"type":"text","text":" Focus on the assignment statement and how it changes state , So you're in functional programming , I won't see it ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"global","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"nolocal","attrs":{}}],"attrs":{}},{"type":"text","text":" The content such as .","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Different ways of writing the same case , Show functional programming ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Concepts and principles are relatively abstract , Let's talk less about concepts , Leave this to the future and summarize it yourself , Direct display of source code differences .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Calculation 1~100 Inside , Calculation 5 And 7 The sum of multiples of ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" Process oriented writing ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"count = 0\nfor num in range(1, 101):\n if num % 5 == 0 or num % 7 == 0:\n count += num\n\nprint(count)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" In process oriented writing , Logic runs from top to bottom , for example ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"num","attrs":{}}],"attrs":{}},{"type":"text","text":" from 1 Count to 100, If the 5 Or right 7 Take remainder equal to 0, That means divisible , And then ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"count","attrs":{}}],"attrs":{}},{"type":"text","text":" With the corresponding ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"num","attrs":{}}],"attrs":{}},{"type":"text","text":" Add up , Get the last remainder .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" This idea is purely process oriented , Usually when we learn programming , The first thing to learn is this kind of writing .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" Object oriented writing ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" There are two ways of writing this kind , One is to use Python Built in list implementation , One is to declare a class to implement .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The first way to write it :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"count = list()\nfor num in range(1, 101):\n if num % 5 == 0 or num % 7 == 0:\n count.append(num)\n\nprint(sum(count))\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" In the above writing , Variable ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"count","attrs":{}}],"attrs":{}},{"type":"text","text":" Make a statement ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"list","attrs":{}}],"attrs":{}},{"type":"text","text":", List objects , But sorting still seems to be the shadow of some procedural programming languages .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" For example, the last ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sum(count)","attrs":{}}],"attrs":{}},{"type":"text","text":" The use of is a little strange , I can't see the shadow of object-oriented .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Next , Let's create a custom class , Carry out logical implementation .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"class My_List_Sum(list):\n def sum(self):\n count = 0\n for n in self:\n count += n\n\n return count\n\n\ncount = My_List_Sum()\nfor num in range(1, 101):\n if num % 5 == 0 or num % 7 == 0:\n count.append(num)\n\nprint(count.sum())\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The above code , We implemented a ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"My_List_Sum","attrs":{}}],"attrs":{}},{"type":"text","text":" class , Let it inherit from ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"list","attrs":{}}],"attrs":{}},{"type":"text","text":", At this point you should understand ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"list","attrs":{}}],"attrs":{}},{"type":"text","text":" It's just a class name , Then it is implemented inside the class ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sum","attrs":{}}],"attrs":{}},{"type":"text","text":" Method , Call the object's ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sum","attrs":{}}],"attrs":{}},{"type":"text","text":" Method , Perfect application of object-oriented writing .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" Let's get to the point , Implementation of functional programming ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Before formal preparation , Need to recall some basic knowledge , for example ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"lambda","attrs":{}}],"attrs":{}},{"type":"text","text":" Add expressions and lists .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Judge a number as 5 perhaps 7 Multiple , ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"lambda","attrs":{}}],"attrs":{}},{"type":"text","text":" It is written as follows :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"multiple = lambda x: x % 5 == 0 or x % 7 == 0\na = multiple(3) # False\nb = multiple(5) # True\nc = multiple(7) # False\nprint(a, b, c)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The list addition code is as follows :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"print([1]+[2]) # [1,2]\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" With the above , You can write a recursive function , Implement the corresponding logic , The description of the code has been added to the comment .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"def tool(n: int, end: int, filter_func) -> list:\n \"\"\" Returns a filtered list \n :param n: Starting value \n :param end: Termination value \n :param filter_func: Judging expressions \n \"\"\"\n # If the upper limit is reached , Return the empty list directly \n if n == end: return []\n # If the filtering conditions are met , Returns a list of this value and the next value \n if filter_func(n):\n return [n] + tool(n + 1, end, filter_func)\n else:\n # The filter conditions are not met , Directly return the next value \n return tool(n + 1, end, filter_func)\n\n\n# Test code \nret = tool(1, 101, lambda x: x % 5 == 0 or x % 7 == 0)\nprint(ret)\nprint(sum(ret))\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The above code is the functional implementation of summation , Some of the logic is as follows :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":" Given the initial and upper values , When the value of the iteration is equal to the upper limit , Return to empty list , That is, the operation ends ;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":" Pass in a judgment condition , In this case, it is a lambda expression , Used to judge 5 and 7 Multiple ;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":" When the conditions are met , What's going on is addition + Iterative work , When the conditions are not met , Go straight to the next iteration .","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Of course, there is also a way to write functional programming , The code is as follows :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"print(sum(n for n in range(1, 101) if n % 5 == 0 or n % 7 == 0))\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The generator used here will be described later .","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Python Features of functional programming ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" stay Python in , Functions are objects , For example, after declaring a function , You can call its properties .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The following code shows the properties of the function object , The rest can be tested by yourself .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"def my_func(var1, var2, **kw):\n return var1 + var2\n\n\nprint(type(my_func)) # \nprint(my_func.__code__)\nprint(my_func.__dict__)\nprint(my_func.__code__.co_code)\nprint(my_func.__code__.co_filename)\nprint(my_func.__code__.co_argcount)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Functional programming is efficient , One of the most important reasons is to delay the calculation , Also called lazy evaluation , These will be gradually expanded later , It's still time to accept the concept of impression .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Because functions are objects , That's why we have the definition of functional programming at the beginning of this article .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Functions can use other functions as arguments , Or return another function , So in the actual coding process , We will convert the function into... In other code “ object ”, So as to realize functional programming .","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Next, let's get in touch with Python The concept and application of pure function in .","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Pure function ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Pure function is a concept , That is, let the function not affect the scope outside the function , That is, the scope is local .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Say something simple. , Is to avoid assignment operations inside functions , It's similar, of course ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"global","attrs":{}}],"attrs":{}},{"type":"text","text":" Keywords such as... Are also avoided .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" For this purpose ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"lambda","attrs":{}}],"attrs":{}},{"type":"text","text":" Expressions are pure functions .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" First look at an example of a pure function :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"def my_func(num: int) -> int:\n return num * 100\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The return value of the function in the above code is only related to ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"num","attrs":{}}],"attrs":{}},{"type":"text","text":" of , The following two conditions are satisfied :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":" No changes to global variables ;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":" The variable data structure was not updated , For example, a list of , Dictionaries .","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" After touching the concept of pure function , Let's take a look at the application of functions as objects .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" stay Python Declare a class in , Some built-in methods will be carried by default , for example :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"from typing import Callable\n\n\n# Declare a class , This class is meaningless , Test use only \nclass Ext:\n # Function passed in , It's portable 1~2 Parameters \n def __init__(self, test_demo: Callable[[int], int]) -> None:\n self.func = test_demo\n\n # The returned result is expanded 2 times \n def __call__(self, arg: int) -> int:\n return self.func(arg) * 2\n\n\ndef one_func(var):\n return var + 1\n\n\ndef two_func(var):\n return var * 3\n\n\ndef three_func(var):\n return var\n\n\na = Ext(one_func)\nprint(a(3)) # 8\n\nb = Ext(two_func)\nprint(b(3)) # 18\n\nc = Ext(three_func)\nprint(c(3)) # 6\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The above code uses a new module ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"typing","attrs":{}}],"attrs":{}},{"type":"text","text":", The module is Python 3.5 After that, the new module , Mainly for Python Provides static type checking .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" In this case, the callback function is imported ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Callable","attrs":{}}],"attrs":{}},{"type":"text","text":", The format is as follows :","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"Callable[[Arg1Type, Arg2Type],ReturnType]\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Where the inner square brackets ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Arg1Type","attrs":{}}],"attrs":{}},{"type":"text","text":" Is a parameter type ,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ReturnType","attrs":{}}],"attrs":{}},{"type":"text","text":" Is the return value type .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The signatures of the above three functions are related to ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Callable","attrs":{}}],"attrs":{}},{"type":"text","text":" Consistency of definition , So it can be used as ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"test_demo","attrs":{}}],"attrs":{}},{"type":"text","text":" The value of the parameter is passed .","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Written in the back ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Snowballing Python The fourth round , A very theoretical series , Keep up with the rhythm of the big army , Walk up , Any questions , You can leave a message in the comment area , commonly 1 It can be solved in an hour .","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Today is the first day of continuous writing 213 / 365 God . Sure Focus on , give the thumbs-up Comment on Collection .","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" More exciting ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" That's too much , Go to the homepage .","attrs":{}}]}]}
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/10/20211014113334574o.html

  1. 请问这个实验如何做(用python3.x的环境)
  2. [algorithm learning] 1486. Array XOR operation (Java / C / C + + / Python / go / trust)
  3. Comment créer un programme Python pour simuler la réservation de sièges dans un cinéma
  4. Comment faire cette expérience (en utilisant l'environnement Python 3.x)
  5. Python之绘制多角星
  6. Pandas的SettingWithCopyWarning报警怎么解决
  7. Comment résoudre l'alarme de réglage avec copywarning de pandas
  8. 使用 Python 进行数据可视化之Matplotlib
  9. Python 常见问题 - 使用 poetry build 打包构建失败,报 ModuleOrPackageNotFound No file/folder found for package
  10. 万圣节奇妙夜,不给糖就捣蛋—Python带你点击解锁恐怖新道具(南瓜头、骷髅、蝙蝠、糖......)
  11. 【算法学习】1221. 分割平衡字符串(java / c / c++ / python / go / rust)
  12. 教你用Python 编写 Hadoop MapReduce 程序
  13. Je t'ai appris à écrire Hadoop mapreduce en Python.
  14. Pandas下载最新版和python不匹配
  15. python tkinter GUI编程如何实现一个时钟,无论主线程怎么卡住,都不影响时钟正常进行走时间。
  16. python tkinter GUI編程如何實現一個時鐘,無論主線程怎麼卡住,都不影響時鐘正常進行走時間。
  17. La programmation de l'interface graphique Python tkinter pour implémenter une horloge n'affecte pas le temps de fonctionnement normal de l'horloge, peu importe comment le fil principal est coincé.
  18. Pandas télécharger la dernière version ne correspond pas à Python
  19. Make a cool music player with Python [V3.0 including source code and packaged exe]
  20. 编写一个函数,可以传入两个实参或三个实参。如果传入两个实参,返回求和。如果传入三个实参,返回的话。前两个实参的和对第三个实参求余的结果Python
  21. python用tcp写server传输文件遇到的问题
  22. Python - - fichiers et exceptions
  23. Python failed to install pyinstaller, preparing wheel metadata... Error
  24. Python Turtle库 画魔法少女里的小圆晓美焰
  25. Quels sont les types de données intégrés de python? Les chiffres.
  26. python代碼實現掃碼關注公眾號登錄
  27. Mise en œuvre du Code Python
  28. 捋一捋Python中的数学运算math库(上篇)
  29. Python:晚上把附近的足浴店都给爬了一遍,好兄弟真不错
  30. python接收字符串,返回其中字符的最后一次出现,并顺序存入列表
  31. python post请求特殊data数据怎么发送
  32. Python: J'ai escaladé tous les salons de foot voisins la nuit. C'est un bon frère.
  33. Retouchez la Bibliothèque math ématiques en Python (Partie 1)
  34. 拓端tecdat:Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列
  35. 拓端tecdat|PYTHON贝叶斯推断计算:用BETA先验分布推断概率和可视化案例
  36. Extension tedat | Python Bayesian Inference Calculation: Inference Probability and Visualization Case Using Beta precedent distribution
  37. python字典里的键是不可变类型,值是可变类型对不对
  38. Extension tedat: Python Prediction and Analysis of Stock Market return time series Using Arima and GARCH Models
  39. Python小白福音!Github开源了一款神器....
  40. Python LITTLE WHITE Gospel! Github a ouvert un artefact...
  41. Opencv Python method for adjusting image contrast and background bleaching of white paper photos with text
  42. 利用Python算法画出美丽动人的妹子图像
  43. 利用Python算法畫出美麗動人的妹子圖像
  44. Utiliser l'algorithme Python pour dessiner de belles images de filles
  45. 关于python程序编写 查找数据
  46. À propos de l'écriture de programmes Python pour trouver des données
  47. python turtle 绘制七段数码管以及14段数码管显示字母和时间
  48. La tortue Python dessine sept segments de tubes numériques et 14 segments de tubes numériques pour afficher les lettres et l'heure
  49. What job can I find after learning Python? Are you ready for employment now?
  50. 关于#python#的问题,请各位专家解答!
  51. 關於#python#的問題,請各比特專家解答!
  52. S'il vous plaît répondre aux questions de Python!
  53. python代码出错,如何修改
  54. python代碼出錯,如何修改
  55. Erreur de code Python, comment modifier
  56. 如何使用Python进行超参调参和调优
  57. Comment utiliser Python pour le réglage hyperparamétrique
  58. 关于#python#的问题,请各位专家解答!
  59. S'il vous plaît répondre aux questions de Python!
  60. Quick start of automation -- python (2) - [string and operator] - half an hour a day