Enumerate some of the fascinating features of Python _python

Source: Internet
Author: User

Here I don't discuss some of Python's useful libraries or frameworks, but talk about the language itself in terms of the language itself, with minimal support. The development of language is getting closer to Lisp, which is why Lisp is a great language.

Let me list the reasons why I study Python:
multi-programming paradigm

Python is a multimodal programming language, called a process-oriented, object-oriented and functional-style combination.

Most people touch the programming language from the beginning of the process, because the procedural mode and the computer operating mode is unified, the instruction sequence and the running process is unified. As a typical C, I also started learning from C, procedural programming language design program is simpler, but in line with the human-computer interaction thinking mode.

Python is an object-oriented language, even "" (space) can also be seen as an object, but the Python competency process is not a problem.

If you do not need to use a static method of a class:

 def a_plus_b (a,b): Return
 a+b

1. Duck Typing

Python is designed to be written as an object-oriented way, not to mention some of the revolutions in object-oriented software design, and one of the highlights in dynamic languages like Python is duck typing (duck type).

About the duck type, that is, if I think an abstract thing can be called "quack", I can take it as a duck.

 def use_duck (duck):
 duck.swim ()
 Duck.gaga ()
 class Duck:
 def swim (self):
 ...
 def gaga (self):
 ...

If you use this:
Little_duck = Duck ()
Use_duck (Little_duck)

About the Duck class, you can give him any name, or inherit it to take another name, just to achieve swim () Gaga () You can think of it as a duck.

As for the duck type, many people do not understand why there is no need to provide an interface to specify the behavior of ducks, I do not support or oppose, my view is this:

    • Check for parameters that do not conform to the characteristics of dynamic languages
    • Provided the interface specification, that is not the duck type, directly called polymorphic

2. Python-Supported functional programming

First is the lambda calculus.

Functional programming is defined as the treatment of a function as a variable, and what is the simplest treatment for a variable in a program?

    • Can be assigned a value
    • Can be used as a parameter
    • Values can be changed (Erlang exception)
    • Not to mention the life cycle and scope.

The lambda calculus contains deep knowledge of computer-related computing, as is the Turing model, a negative answer to the problem of downtime, not just as simple as an anonymous function.

About the lambda calculus, see what this program does:

 Map (Lambda n:2*n,[1,2,3,4,5])

Lambda N:2*n itself as an anonymous function
The lambda itself passes in as a parameter to the map () function, which means that my higher order function converts a function to a variable as a parameter, which is the higher treatment it receives as a function.

There are two ways to assign values and change values:

f = Fun () does not change the function state, only the name, but the description function can be assigned a value
You can use closures as a way to change the state of a function, or use an adorner to complete a function state change

The use of functional programming can also improve the readability of the program and reduce the code, and can clearly express the function of functions, such as MapReduce is from the idea of functional programming:

 Map (Func,list)

The effect is to func each 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 way brings us.

Of course, functional programming is not a few words to say, understand the core of functional programming is to understand the lambda calculus.
Some interesting features.

1. Inert calculation:

Take a look at the completion of a Fibonacci sequence of what Python can do:

 >>> def fib ():
   A, b = 0, 1 while
   1:
     yield b
     A, B = B, a+b
 
 >>> f = fib ()

In fact, an iterative object is generated by yield, and each call to F.next () produces a Fibonacci value, and the internal state of the function is stored by the iteration object. As for returning an iterative object, you can use Itertools.islice if you need to determine how many bits to iterate.

2. Co-process

The coprocessor is also a concept based on yield, the main pattern is the micro-threading Collaborative mode of work:

 def coroutine (func):
   def ret ():
     f = func ()
     f.next () return
     F return
   ret
 
 @coroutine
 def consumer ():
   print "Wait for getting a task"
   while 1:
     n = (yield)
     print "Got%s", N
  
 impor T time
 def producer ():
   C = consumer () while
   1:
     time.sleep (1)
     print "Send a task to consumer" C26/>c.send ("task")
  
 if __name__ = = "__main__":
 producer ()

The benefit of the coprocessor is that you can schedule your thread directly, which is why it is called a coprocessor and not a thread, which is a preemptive concurrency, and a coprocessor is a collaborative concurrency.
the benefits of dynamic language

In terms of the thrill of programming (I believe only the feeling of the person who loves it), dynamic languages, such as Python, save more time to spend with girlfriends or wives, or husbands.

Of course, as the rapid development of the internet age, duck on the line, but also "hackers and painters" described above, rapid development is very important, of course, need to meet this demand.

The CPU-intensive operations of a dynamic language are necessarily more than C + +.

Anyway: Life is short, I use Python.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.