seller
How does Python manage memory?

A: From three aspects, one object reference counting mechanism, two garbage collection mechanism, three memory pool mechanism

___________
Object reference counting mechanism

Python uses reference counting internally to keep track of objects in memory, and all objects have reference counting.

When the reference count increases:

An object is assigned a new name

Put it into a container (such as a list, tuple, or dictionary)

When the reference count is reduced:

Destruction of the object alias display using the DEL statement

A reference is out of scope or reassigned

The sys.getrefcount() function gets the current reference count of an object

In most cases, the reference count is 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 save memory.

2.
The garbage collection

When an object’s reference count reaches zero, it is disposed of by the garbage collection mechanism.

When two objects A and B refer to each other, the DEL statement reduces the reference count of a and B and destroys the name used to refer to the underlying object. However, since each object contains an application to another object,

So the reference count does not go to zero and the object is not destroyed. (resulting in memory leaks). To solve this problem, the interpreter periodically executes a loop detector that searches for loops of unreachable objects and removes them.

3.
Memory pool mechanism

Python provides a garbage collection mechanism for memory, but it puts unused memory into the memory pool rather than returning it to the operating system.

Pymalloc mechanism. To speed up Python’s execution, Python introduced a memory pool mechanism to manage the allocation and release of small chunks of memory.

All objects smaller than 256 bytes in Python use pymalloc’s allocator, while large objects use the system’s Malloc.

Python objects such as integers, floating-point numbers, and lists have separate private memory pools, and objects do not share their memory pools. That is, if you allocate and free a large number of integers, the memory used to cache them can no longer be allocated to floating point numbers.



Denominated in
What is a lambda function? What’s it good for?

A: Lambda expressions are usually used when a function is needed but you don’t want to bother naming a function, i.e., anonymous functions

Lambda functions: The primary use is to point to short callback functions

lambda [arguments]:expression

>>> a=lambdax,y:x+y

> > > a (3, 11)



an
How to convert a tuple to a list in Python?

A: Use the tuple and list functions directly. Type () determines the type of the object

registering
Write a Python code that removes duplicate elements from a list

A:

1. Set (list)

2. Use the dictionary function

> > > a =,2,4,2,4,5,6,5,7,8,9,0 [1]

>>> b={}

>>>b=b.fromkeys(a)

>>>c=list(b.keys())

>>> c



All measures
The program sorts with sort and then judges from the last element

A =,2,4,2,4,5,7,10,5,5,7,8,9,0,3 [1]

a.sort()

last=a[-1]

for i inrange(len(a)-2,-1,-1):

if last==a:

del a

else:last=a

print(a)



How do I copy an object in Python? (Assignment, shallow copy, deep copy difference)

A: Assignment (=) creates a new reference to an object, and modifying one of the variables affects the other.

Shallow copy: create a new object that contains a reference to the items contained in the original object (if you modify one object by reference, the other object will also change) {1, the full slice method; 2. factory functions, such as list(); Copy ()}

Deep copy: Creates a new object and recursively copies its contents (modifying one, leaving the other unchanged) {copy module’s deep.deepcopy() function}



How to use and function except?

A: try… Except… Except… [else]… [finally…

Execute the statement under try, or skip to the except statement if an exception is thrown. Execution is attempted sequentially for each EXCEPT branch, and if the exception raised matches the exception group in an EXCEPT, the corresponding statement is executed.

If all except do not match, the exception is passed to the highest-level try code that calls this code next.

The statement under try executes normally, else block code is executed. If an exception occurs, it will not be executed

If a finally statement is present, it is always executed.



What does the pass statement do in Python?

A: whileFalse:pass statements do not perform any operations and are generally used as placeholders or to create placeholders



How to use range() in Python?

Answer: List a set of data, often used in for in range() loops



How do I query and replace a text string in Python?

A: You can use the sub() or subn() functions in the RE module to query and replace,

Format: sub(replacement, string[,count=0]) (replacement is the text to be replaced, string is the text to be replaced, and count is an optional parameter, indicating the maximum number of replacements)

>>> import re

> > > p=re.com running (‘ blue | white | red ‘)

>>> Print (p.sub(‘ colour ‘,’blue socks and red shoes’))

colour socks and colourshoes

>>>print(p.supu (‘ colour ‘,’blue socks and red shoes’,count=1))

colour socks and redshoes

The subn() method performs the same as sub(), except that it returns a two-dimensional array containing the new string after the substitution and the total number of substitutions

What is the difference between match() and search()?

A: Match (pattern,string[,flags]) in the RE module, check whether the beginning of string matches the pattern.

Re.search (pattern,string[,flags]) in the re module, searches for the first matching value of pattern in string.

> > > print (re. Match (‘ super ‘, ‘superstition’) span ())

(0, 5)

> > > print (re) match (‘ super ‘, ‘insuperable))

None

> > > print (re search (‘ super ‘, ‘superstition’) span ())

(0, 5)

> > > print (re search (‘ super ‘, ‘insuperable.) span ())

(2, 7)

<.*> and <.*? What’s the difference?

A: The terms are greedy matching (<.* >) and non-greedy matching (<.*? >)

Such as:

test

< * > :

test

“. *? > :



How to generate random numbers in Python?

A: Random module

Randint (a,b) : Returns a random integer x,a<=x<=b

Random.randrange (start,stop,[,step]) : Returns a random integer between (start,stop,step), excluding the end value.

Random. Random (): Returns a floating point number between 0 and 1

Random. Uniform (a,b): Returns floating point numbers in the specified range.



Is there a tool to help find Python bugs and do static code analysis?

A: PyChecker is a static analysis tool for Python code. It helps find bugs in Python code and warns about complexity and formatting

Pylint is another tool that can do codingStandard checking



How to set a global variable in a function?

A: The solution is to insert a global declaration at the beginning of function:

def f()

global x



(16) the difference between single quotation marks, double quotation marks and triple quotation marks

A: Single and double quotation marks are equivalent. If you want to wrap a line, you need a symbol (\). Triple quotation marks can wrap a line directly and can contain comments

If I wanted to represent the string Let’s go

Single quotation marks: s4 = ‘Let\’ go ‘

Double quotation marks: s5 = “Let’s go”

S6 = ‘I realy like “python”! ‘

This is why both single and double quotation marks can represent strings

For more technical information on Python, see itheimaGZ