In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the differences between Python2 and Python3". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what's the difference between Python2 and Python3?"
What is the duck typing type?
Duck type is the method used by dynamically typed language to determine whether an object is of a certain type or not, also known as duck determination. To put it simply, the type of duck means to judge whether a bird is a duck or not. we only care about whether it swims like a duck, whether it barks like a duck, and whether it walks like a duck. In other words, if the behavior of the object is consistent with our expectations (being able to accept certain messages), we assume that it is a certain type of object.
In Python language, there are many bytes-like objects (such as bytes, bytearray, array.array, memoryview), file-like objects (such as StringIO, BytesIO, GzipFile, socket), path-like objects (such as: str, bytes), among which file-like objects can support read and write operations, and can read and write like files. This is the so-called method that objects can be judged as ducks if they have duck behavior. Another example is the extend method of a list in Python, which requires parameters that do not have to be a list, as long as they are iterable objects.
Description: the duck type of dynamic language greatly simplifies the application of design patterns.
Talk about the scope of variables in Python.
There are four scopes in Python, namely, local scope (Local), nested scope (Embedded), global scope (Global), and built-in scope (Built-in). When searching for an identifier, it will be searched in the order of LEGB, and if the identifier is not found in all scopes, a NameError exception will be thrown.
Topic 28: tell me about your understanding of closures.
Closure is a technique for lexical binding in programming languages (Python, JavaScript, etc.) that support first-class functions. When a closure is captured, its free variables (variables defined outside the function but used inside the function) are determined at capture time, so that it can run as usual even if it is out of context at the time of capture. In a nutshell, a closure can be understood as a function that can read internal variables of other functions. In this case, the local variable of the function ends its life cycle after the function call, but the closure extends the life cycle of the local variable. When using closures, it should be noted that closures will prevent objects created in the function from being garbage collected, which may cause a lot of memory overhead, so closures must not be abused.
Topic 29: talk about the application scenarios, advantages and disadvantages of multi-threading and multi-process in Python.
Thread is the basic unit for operating system to allocate CPU, and process is the basic unit for operating system to allocate memory. Usually the program we run contains one or more processes, and each process contains one or more threads. The advantage of multithreading is that multiple threads can share the memory space of the process, so the communication between processes is very easy to achieve; but if you use the official CPython interpreter, multithreading is subject to GIL (global interpreter lock) and can not take advantage of the multi-core nature of CPU, which is a big problem. Using multi-processes can take full advantage of the multi-core characteristics of CPU, but inter-process communication is relatively troublesome and requires the use of IPC mechanisms (pipes, sockets, etc.).
Multithreading is suitable for applications that spend a lot of time on Icano operations, but do not require too much parallel computing and do not require too much memory. Multi-processes are suitable for performing compute-intensive tasks (such as video coding and decoding, data processing, scientific computing, etc.), tasks that can be decomposed into multiple parallel subtasks and can combine the execution results of subtasks, and tasks that have no restrictions on memory usage and do not strongly rely on IMago operations.
Extension: there are usually three options for concurrent programming in Python: multi-thread, multi-process and asynchronous programming. Asynchronous programming realizes collaborative concurrency and realizes the efficient use of CPU through the user-mode switching of several cooperative subroutines, which is also very suitable for Icano-intensive applications.
Talk about the difference between Python 2 and Python 3.
Comments: this kind of question must not recite the so-called reference answer. It is enough to say something you are most familiar with.
Print and exec in Python 2 are keywords and become functions in Python 3.
There is no long type in Python 3, and integers are all int types.
The unequal sign in Python 2 is discarded in Python 3 and is used uniformly.
The xrange function in Python 2 is replaced by the range function in Python 3.
Python 3 improves the unsafe input function in Python 2, abandoning the raw_input function.
The file function in Python 2 is replaced by the open function in Python 3.
The / operation in Python 2 is an integral division for the int type, and / / is used to do the integral division in Python 3.
The code for catching exceptions in Python 2 has been improved in Python 3, and it is obvious that Python 3 is written in a more reasonable way.
The scope of the loop variable in the Python 3 generator is better controlled and does not affect the variables of the same name outside the generator.
The round function in Python 3 can return the int or float type, and the round function in Python 2 returns the float type.
The str type of Python 3 is a Unicode string, and the str type of Python 2 is a byte string, which is equivalent to bytes in Python 3.
The comparison operator in Python 3 must compare similar objects.
The classes defined in Python 3 are all new classes. The classes defined in Python 2 can be divided into new classes (classes explicitly inherited from object) and old classes (classical classes). There is a very significant difference between new classes and old classes on the MRO problem. New classes can use the _ _ class__ attribute to get their own types, and new classes can use _ _ slots__ magic.
Python 3 has more stringent requirements for code indentation, and mixing spaces and tabulation keys will cause TabError.
The keys, values, items methods of the dictionary in Python 3 no longer return the list object, but return view object, and the built-in map, filter and other functions no longer return the list object, but the iterator object.
The names of some modules in the Python 3 standard library are different from Python 2; in terms of three-party libraries, some third-party libraries only support Python 2, and some only support Python 3.
At this point, I believe you have a deeper understanding of "what's the difference between Python2 and Python3". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.