In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the use of Python can do what the relevant knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone reading this article can do what Python will gain, let's take a look at it.
I. multiple programming paradigm
Python is a multi-paradigm programming language that combines procedural, object-oriented, and functional.
Most people come into contact with programming languages from the beginning of the procedural, because the procedural program mode and the computer operation mode are unified, and the instruction sequence and the operation process are unified.
Such as the typical C, I also started learning from C, procedural programming language design program is relatively simple, but in line with human-computer interaction thinking.
Python is an object-oriented language, and even " "(space) can be considered an object, but Python is competent for procedural expressions without problems.
If you do not need to use static methods of the class:
def a_plus_b(a,b): return a+b
Python is written as an object-oriented way when it is designed, not to mention some revolutions brought by object-oriented software design, etc. In a dynamic language like python, there is a bright spot in object-oriented is Duck typing.
The duck type, 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 in this way
little_duck Duck() use_duck( little_duck )
For the Duck class, you can give it any name you want, or inherit it with another name, just implement swim() gaga() and you can treat it like a duck.
Regarding duck types, many people don't understand why it is not necessary to provide an interface to specify duck behavior. I neither support nor oppose it. My point is this:
1. Check for parameters that do not conform to the characteristics of dynamic languages
2. If the interface specification is provided, then it is not a duck type, and it is directly called polymorphism.
About Python Supported Functional Programming
First, lambda calculus.
The definition of functional programming is to treat functions as variables. What is the simplest treatment for variables in a program?
1. can be assigned
2. Can be used as a parameter
3. Values can be changed (Erlang exception)
4. Not to mention life cycles and scopes.
Lambda calculus contains deep knowledge of computability of computers. Lambda is also a Turing model, which is a negative answer to the halt problem. is not just an anonymous function.
So about lambda calculus, look at what this program does.
map(lambda n:2*n,[1,2,3,4,5])
1. lambda n:2*n itself as an anonymous function
2. lambda itself is passed as an argument to the map() function, which means that my higher order function can be transformed into a variable passed 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 changes the name, but shows that the function can be assigned
2. Closures can be used as a way to change the state of a function, or decorators can be used to change the state of a function
The use of functional programming can also improve the readability of the program and reduce the code, and can clearly express the function of the function, such as MapReduce is from the functional programming idea.
Map(func,List)
The effect is to apply func to every element in the List
Take the example just given
map(lambda n:2*n,[1,2,3,4,5])
This function returns: [2,4,6,8,10]
It's important to know that this approach gives us clarity about design.
Of course, functional programming is not so a few words to finish, the core of understanding functional programming is to understand λ calculus
II. Some interesting properties
inertia calculation:
See what Python can do to complete a Fibonacci sequence:
>>> def fib(): a , b = 0 ,1 while 1: yield b a , b = b ,a+b >>> f = fib()
yield actually generates an iterable object, and each time it's called
f.next () produces a Fibonacci value, and the internal state of the function is stored by the iteration object.
As for returning an iterable object, if you need to determine how many bits to iterate over, you can use itertools.islice
Coordination:
Coroutine is also a yield-based concept, and the main pattern is the collaborative working pattern of microthreads:
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 coroutines is that you can schedule your threads directly, which is why they are called coroutines rather than threads. Threads are preemptive concurrency, coroutines are collaborative concurrency.
III. Benefits of Dynamic Language
In terms of the thrill of programming (which I believe is only for those who love it), dynamic languages, such as python, save more time to spend with your girlfriend or wife or husband
Of course, as for rapid development in the Internet era, catching ducks online is also introduced in "Hackers and Artists." Rapid development is very important. Of course, it needs to meet the needs in this regard.
CPU-intensive operations in dynamic languages are inevitably inferior to C/C++
So much for "What you can do with Python", thanks for reading! I believe everyone has a certain understanding of "what can be done with Python" knowledge. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.