List some attractive features in Python and list python
Here I will not discuss some useful libraries or frameworks of python. I will talk about this language only when the language itself is the least supported. Language Development is getting closer and closer to Lisp, which is also the reason for the greatness of the language.
The following lists the reasons why I learned python:
Multi-Programming Paradigm
Python is a multi-paradigm programming language. It is called a process-based, object-oriented, and functional combination.
Most people start from procedural programming languages because procedural programming methods are consistent with computer running methods, and command sequences and running processes are consistent. For example, in typical C, I also started to learn from C. The process-based programming language design is relatively simple, but it is in line with human-computer interaction.
Although python is an object-oriented language, even "" (Space) can be seen as an object, but python is competent in the process.
If you do not need to use static methods of the class:
def a_plus_b(a,b): return a+b
1. Duck typing
When designing Python, it is written as an object-oriented method, not to mention some revolutions brought by object-oriented software design, in a dynamic language like python, one of the highlights of object-oriented is Duck typing (Duck type ).
Regarding the duck type, that is to say, if I think an abstract thing will be swimming, it will be "grack" and I can treat it as a duck.
def use_duck( Duck ): Duck.swim() Duck.gaga() class Duck: def swim(self): ... def gaga(self): ...
If you use this method:
Little_duck = Duck ()
Use_duck (little_duck)
For the Duck class, you can give it any name, or inherit it to get another name. You only need to implement swim () gaga () And you can treat it as a Duck.
Many people do not understand why they do not need to provide an interface to regulate the behavior of ducks. I neither support nor disagree. My point is as follows:
- The parameter check does not conform to the dynamic language features.
- If the interface specification is provided, it is not a duck type. It is called polymorphism.
2. Functional programming supported by Python
The first is lambda calculation.
The definition of functional programming is to treat a function as a variable. What is the simplest treatment of a variable in a program?
- Value assignment allowed
- Can be used as a parameter
- Value can be changed (except Erlang)
- Not to mention the lifecycle and scope.
Lambda is also a Turing model and a negative answer to the downtime problem. It is not just as simple as an anonymous function.
For lambda calculus, see what this program has done:
map(lambda n:2*n,[1,2,3,4,5])
Lambda n: 2 * n itself acts as an anonymous Function
Lambda itself is passed into the map () function as a parameter. That is to say, my high-order function can regard the function transformation as a variable passed as a parameter, which is also a high level of treatment it receives as a function.
There are two ways to assign values and change values:
F = fun () does not change the function state, but only the name, but it indicates that the function can be assigned a value.
You can use closures to change the state of a function or decorator to change the state of a function.
The use of functional programming can also improve program readability and reduce code, and clearly express function functions. For example, MapReduce is the idea of functional programming:
Map(func,List)
The function is to apply func to every element in the List.
Take the example just now:
map(lambda n:2*n,[1,2,3,4,5])
This function returns
[2,4,6,8,10]
It is important to know the clear design that this method brings to us.
Of course, functional programming is not just a few words. The core of understanding functional programming is to understand the lambda algorithm.
Some interesting features
1. inert computing:
Let's see how python can complete a Fibonacci series:
>>> def fib(): a , b = 0 ,1 while 1: yield b a , b = b ,a+b >>> f = fib()
In fact, yield generates an iteratable object. Each time f. next () is called, a Fibonacci value can be generated, and the internal state of the function is stored by the iterative object. To return an iteratable object, you can use itertools. islice to determine the number of iterations.
2. coroutine
Coroutine is also a concept based on yield. The main mode is the collaborative working mode of micro-threads:
def coroutine(func): def ret(): f = func() f.next() return f return ret @coroutine def consumer(): print "Wait to getting a task" while 1: n = (yield) print "Got %s",n import time def producer(): c = consumer() while 1: time.sleep(1) print "Send a task to consumer" c.send("task") if __name__ == "__main__": producer()
The benefit of coroutine is that you can directly schedule your thread, which is also why it is called coroutine rather than thread. The thread is preemptible concurrency, And the coroutine is collaborative concurrency.
Benefits of Dynamic Language
From the pleasure of programming (I believe only those who love it feel), dynamic languages, such as python, save more time to spend with girlfriends or wives, or husband.
Of course, as for the rapid development of the Internet age, catching up with ducks is also described in hacker and painter. rapid development is very important. Of course, it must meet this requirement.
Dynamic Language CPU-intensive operations are bound to be less than C/C ++.
In short: My life is short. I use python.