How is memory management done?
A: From three aspects, an object reference counting mechanism, two garbage collection mechanism, three memory pool mechanism
I. Reference Counting Mechanism for Objects
Python uses reference counting internally to keep track of objects in memory, and all objects have reference counts.
The case where the reference count increases:
1, an object is assigned a new name
2, put it in a container (such as a list, tuple or dictionary)
The case where the reference count is reduced:
1, the use of the del statement on the object alias display destruction
2, references are out of scope or reassigned
The ( ) function gets the current reference count of an object
In most cases, reference counts are much larger than you might guess. For immutable data (such as numbers and strings), the interpreter shares memory in different parts of the program to conserve memory.
II. Garbage collection
1, when the reference count of an object goes to zero, it will be disposed of by the garbage collection mechanism.
2, when two objects a and b reference each other, the del statement reduces the reference counts of a and b and destroys the names used to reference the underlying objects. However, since each object contains an application to the other object, the reference count does not go to zero and the objects are not destroyed. (thus leading to a memory leak). To solve this problem, the interpreter periodically executes a loop detector that searches for loops of inaccessible objects and removes them.
III. Memory pool mechanism
Python provides a garbage collection mechanism for memory, but it puts unused memory into a memory pool instead of returning it to the operating system.
1, Pymalloc mechanism. In order to speed up the execution efficiency of Python, Python introduces a memory pool mechanism to manage the request and release of small chunks of memory.
2, all objects in Python smaller than 256 bytes use the pymalloc implementation of the allocator, while larger objects use the system malloc.
3. Python objects such as integers, floats, and lists have their own private memory pools, and objects do not share their pools. This means that if you allocate and free a large number of integers, the memory used to cache them cannot be reallocated to floats.
2. what is lambda function? What are its benefits?
A: A lambda expression, usually used when you need a function but don't want to bother naming one, i.e., an anonymous function.
lambda functions: the primary use is to point to short callback functions
lambda [arguments]:expression >>> a=lambdax,y:x+y >>> a(3,11)
How to implement tuple and list conversion inside?
A: directly use the tuple and list function on the line, type () can determine the type of the object
4. Write a Python code to remove duplicate elements from a list.
Answer:
1, use the set function, set(list)
2, using the dictionary function.
>>>a=[1,2,4,2,4,5,6,5,7,8,9,0] >>> b={} >>>b=(a) >>>c=list(()) >>> c
5. program to sort by sort and then start judging from the last element
a=[1,2,4,2,4,5,7,10,5,5,7,8,9,0,3] () last=a[-1] for i inrange(len(a)-2,-1,-1): if last==a[i]: del a[i] else:last=a[i] print(a)
How to copy an object inside? (Difference between assignment, shallow copy, deep copy)
A: Assignment (=), is the creation of a new reference to an object, and modifying either of these variables affects the other.
Shallow copy: creates a new object, but it contains references to items contained in the original object (if you modify one of the objects by reference, the other will also be modified) {1, full slice method; 2, factory functions such as list(); 3, copy() function of the copy module}
Deep copy: creates a new object and recursively copies the objects it contains (modifies one, leaves the other unchanged) {() function of the copy module }
7. Describe the use and function of except?
A: try...except...except...[else...][finally...]
Execute the statements under try, and if an exception is raised, execution jumps to the except statement. Execution is attempted sequentially for each except branch, and if the exception raised matches the exception group in the except, the appropriate statement is executed. If none of the excepts match, the exception is passed to the next highest level of try code that calls this code.
The statement under try is executed normally, then the else block of code is executed. If an exception occurs, the
If a finally statement exists, it will always be executed at the end.
What is the purpose of the pass statement in the
A: The pass statement does not perform any operations and is generally used as a placeholder or to create a placeholder program whileFalse:pass
9.Describe the usage of range() function under Python?
A: List a set of data, often used in for in range() loops
10. How to query and replace a text string using Python?
A: You can use the sub() function or the subn() function in the re module to query and replace.
Format:sub(replacement, string[,count=0])(
(replacement is the text to be replaced with, string is the text to be replaced with, count is an optional parameter that refers to the maximum number of replacements)
>>> import re >>>p=(‘blue|white|red') >>>print((‘colour','blue socks and red shoes')) colour socks and colourshoes >>>print((‘colour','blue socks and red shoes',count=1)) colour socks and redshoes
The subn() method performs the same effect as sub(), but it returns a two-dimensional array containing the new string after replacement and the total number of replacements
Difference between match() and search() in there?
A: re module match(pattern, string[,flags]), check whether the beginning of the string matches the pattern.
re module in the search(pattern, string[,flags]), in the string to search for the first match of the pattern.
>>>print((‘super', ‘superstition').span()) (0, 5) >>>print((‘super', ‘insuperable')) None >>>print((‘super', ‘superstition').span()) (0, 5) >>>print((‘super', ‘insuperable').span()) (2, 7)
12. When using Python to match HTML tags, <. *> and <. *? > what is the difference?
A: The terms called greedy matching ( <. *> ) and non-greedy matching ( <. *? > )
Example.
test
<.*> :
test
<.*?> :
How do you generate random numbers inside?
A: random module
Random integer: (a,b): return random integer x,a<=x<=b
(start,stop,[,step]): return a random integer in the range between (start,stop,step), excluding the end value.
Random real number: ( ): returns a floating point number between 0 and 1
(a,b): returns a floating point number in the specified range.
14. Is there a tool to help find python bugs and do static code analysis?
A: PyChecker is a static analysis tool for python code, which helps to find bugs in python code, and warns about the complexity and formatting of the code.
Pylint is another tool to do codingstandard checking.
15. How to set a global variable inside a function?
A: The solution is to insert a global declaration at the beginning of the function:
def f()
global x
16. The difference between single quotes, double quotes, and triple quotes
A: single quotes and double quotes are equivalent, if you want to line breaks, you need the symbol (\), triple quotes can be directly line breaks, and can contain comments
If you want to represent Let's go, the string
Single quote: s4 = 'Let\'s go'
Double quotes: s5 = "Let's go"
s6 = ‘I realy like“python”!'
That's how both single and double quotes can represent strings
summarize
The above mentioned is the latest Python Interview Questions and Answers 2019 Python 16 questions, I hope it will help you, if you have any questions please leave me a message, I will reply to you in time. I would also like to thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!