Functional Programming:
One of the features of functional programming is that it allows the function itself to be passed as a parameter to another function, and also allows a function to be returned!
Python has the map () and reduce () functions built into it.
Map (): The function receives two parameters, one is a function, the other is a sequence, and the map functions the incoming function to each element of the sequence sequentially and returns the result as a new list. Example: There is a function f (x) =x^2, in order to function in a list [1, 2, 3, 4, 5, 6, 7, 8, 9], you can use map () to achieve the following:
>>>map (F, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[1, 4, 9, 16, 25, 36, 49, 64, 81]
Reduce (): Put a function in a sequence [X1, x2, x3 ...] , the function must receive two parameters, and reduce calculates the result and the next element of the sequence, and the effect is:
Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)
>>> def fn (x, y): ...
return x * ten + y ...
>>> reduce (FN, [1, 3, 5, 7, 9])
13579
Filter (): Similar to map (), it also receives a function and a sequence. When different from map (), filter () applies the incoming function to each element sequentially, and then decides whether to persist or discard the element based on whether the return value is true or false.
In a list, delete even numbers, keep only odd numbers, and you can write:
def is_odd (n):
return n% 2 = = 1
Filter (is_odd, [1, 2, 4, 5, 6, 9, 10, 15]) # results: [1, 5, 9, 15]
Sorted (): The function can sort the list, sorted ([36, 5, 12, 9, 21]). It is also a higher-order function that can receive a comparison function to implement a custom sort. For example, if you want to sort in reverse order, you can customize a reversed_cmp function:
def reversed_cmp (x, y):
If x > Y:
Return-1
If x < y:
Return 1
return 0
By passing in a custom comparison function reversed_cmp, you can sort in reverse order:
>>> Sorted ([36, 21, 12, 9, 5], reversed_cmp) [[5, 9]
Object-Oriented Programming
class :
In Python, all data types can be treated as objects and, of course, objects can be customized.
Class Student (object):
def __init__ (self, Name, score):
Self.name = Name
Self.score = Score
#Student类名通常是大写开头的单词, followed by (object), indicates which class the class inherits from, and generally, if there is no suitable inheriting class, use the object class, which is the class that all classes will eventually inherit. The first argument of the __init__ method is always self, which represents the created instance itself, so within the __init__ method, various attributes can be bound to self, because the "to" instance itself points to the creation of a #. With the __init__ method, when you create an instance, you cannot pass in an empty argument, you must pass in a parameter that matches the __init__ method, but self does not need to be passed, and the Python interpreter will pass in the instance variable itself.
The function defined in the class is only a little different from the other functions, that is, the first argument is always the instance variable self and, when called, does not pass the argument. In addition, there is no difference between a class's method and a normal function, which can still be used with default parameters, variable parameters, and keyword parameters.
External code is also free to modify the name, score property of an instance
>>> bart = Student (' Bart Simpson ', 98)
>>> Bart.score
98
>>> Bart.score = 59
If you want the internal properties to be inaccessible externally, you can add two underscores to the name of the property. Self.__name = Name,self.__score = Score
In Python, the variable name of an instance begins with __, and becomes a private variable (private) that can only be accessed internally and cannot be accessed externally.
In Python, the variable name is like __xxx__, which starts with a double underscore, and ends with a double underscore, which is a special variable that can be accessed directly, not by a private variable, or by a variable name such as __name__ or __score__.
polymorphic:
When we define a class, it is equivalent to defining a data type. The data types we define are the same data types that python comes with, such as STR, list, dict:
Determining whether a variable is a type can be judged by isinstance ():
>>> isinstance (A, list)
True or False
When invoking a class instance method, the variable is treated as a parent class type, so that all subclass types can be received normally.
Python Learning Notes (iii)