List some attractive features in Python

  • 2020-05-07 20:00:40
  • OfStack

I won't discuss any useful libraries or frameworks of python here, but only the language itself, with minimal support. Language development is getting closer and closer to Lisp, which is why Lisp is so great.

Below I list 1 reasons why I studied python:
multi-programming paradigm

python is a multi-paradigm programming language that combines so-called procedural, object-oriented, and functional methods.

Most people contact programming language from the process of the beginning, the reason is that the process of the program and the way the computer runs is unified, the instruction sequence and the running process is unified. For example, C is a typical program, which I started to learn from C. The procedural programming language design and programming is relatively simple, but it conforms to the thinking mode of human-computer interaction.

Although python is an object-oriented language, even "" (space) can be regarded as an object, but python competency is no problem.

If you do not need to use static methods of the class:


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

1. Duck typing

Python is designed as an object-oriented way to write, apart from the object-oriented to the software design revolution, python such as the dynamic language has a highlight is Duck typing (duck).

About duck types, that is, if I think an abstract thing can swim and quack, I can think of it as a duck.


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

If used like this:
  little_duck = Duck()
  use_duck( little_duck )

With the Duck class, you can give it any name, or inherit it to another name, just implement swim() gaga() and you can treat it like a duck.

As for duck types, many people do not understand why there is no need to provide an interface to regulate duck behavior. I neither support nor object to it. My opinion is as follows:

      checks for parameters that do not conform to the characteristics of dynamic languages       provides the interface specification, which is not a duck type, just polymorphic

2. Python supports functional programming

The first is the lambda calculus.

The definition of functional programming is to treat a function as if it were a variable. What is the simplest treatment of a variable in a program?

      can be assigned       can be used as a parameter       can change values (Erlang exception)       apart from the lifecycle and scope

With a deep knowledge of computer computability behind the lambda calculus, lambda is also a Turing model, a negative answer to the downtime problem, not just an anonymous function.

For 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
      lambda itself is passed into the map() function as a parameter, which means that my higher-order function can be regarded as a variable to pass as an argument, which is also the higher treatment it receives as a function

There are two ways to assign and change values:

      f = fun() does not change the state of the function, only the name, but indicates that the function can be assigned
      can use closures as a way to change the state of a function, or decorators to do so

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


 Map(func,List)

The effect is to apply func to every element of List.

Take this example:


 map(lambda n:2*n,[1,2,3,4,5])

This function returns


 [2,4,6,8,10]

It is important to know that this approach gives us a clear way to design.

Of course, functional programming isn't all in a few sentences. At the heart of understanding functional programming is understanding the lambda calculus.
1 some interesting features

1. Lazy calculation:

See what python can do to complete 1 Fibonacci sequence:


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

One iterable object is actually generated by yield, with one Fibonacci value per call to f.next (), and the internal state of the function is stored by the iterated object. As for returning 1 iterable object, if you need to determine how many bits to iterate to, you can use itertools.islice.

2.

Coroutine is also a concept based on yield. The main mode is the collaborative working mode of tasklets:


 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 schedule your threads directly, which is why it's called a coroutine instead of a thread, which is preemptive concurrency, and coroutine is collaborative concurrency.
Benefits of dynamic language

In terms of the thrill of programming (which I'm sure only those who love it), dynamic languages like python save more time to spend with a girlfriend or wife or husband.

Of course, as the rapid development of the Internet era, catch the ducks online, is also "hackers and painters" above the introduction, rapid development is very important, of course, need to meet the needs of this aspect.

CPU intensive operations in dynamic languages are no match for C/C++.

Anyway: life is short. I use python.


Related articles: