Python small face question

Source: Internet
Author: User
Tags garbage collection

How does python perform memory management?
Python introduces a mechanism: reference counting.
Python internally uses reference counting to keep track of objects in memory, and Python internally records how many references the object has, that is, the reference count, when the object is created, a reference count is created, and when the object is no longer needed, the object has a reference count of 0 o'clock, which is garbage collected.
Summarize the object in the case of a reference count plus 1:
1. Object created: x=4
2. Other people were created: y=x
3. Passed as a parameter to the function: Foo (x)
4. As an element of the container object: a=[1,x, ' 33 ']
Reference count Reduction Scenario
1. A local reference has left its scope. For example, at the end of the foo (x) function above, X points to the object reference minus 1.
2. The alias of the object is explicitly destroyed: Del x; or del y
3. An alias of an object is assigned to another object: x=789
4. Object is removed from a Window object: Mylist.remove (x)
5. The Window object itself is destroyed: Del myList, or the Window object itself is out of scope.

Garbage collection
1. When there are no longer parts in memory, the garbage collector will clean them off. It checks for those objects that have a reference count of 0, and then clears its memory space. Of course, in addition to the reference count of 0 will be cleared, there is also a situation will be cleared by the garbage collector: when two objects refer to each other, their own other references are already 0.
2. Garbage collection mechanism There is also a recycle garbage collector, which ensures that the circular reference object is released (a reference B, B refers to a, which causes its reference count to never be 0).

In Python, many times the requested memory is small chunks of memory, these small pieces of memory after the application, will soon be released, because these memory applications are not to create objects, so there is no object-level memory pool mechanism. This means that Python performs a large amount of malloc and free operations during runtime, frequently switching between user state and kernel mentality, which can seriously affect Python's execution efficiency. To speed up the execution of Python, Python introduces a memory pooling mechanism for managing the application and release of small chunks of memory.
Memory pool mechanism

Python provides a garbage collection mechanism for memory, but it puts unused memory into the memory pool instead of returning it to the operating system.
All objects less than 256 bytes in Python Use the allocator implemented by Pymalloc, while large objects use the system malloc. In addition, Python objects, such as integers, floating-point numbers, and lists, have their own private pools of memory that do not share their pools of memory among objects. That is, if you allocate and release a large number of integers, the memory used to cache these integers can no longer be assigned to floating-point numbers.

In Python, many times the requested memory is small chunks of memory, these small pieces of memory after the application, will soon be released, because these memory applications are not to create objects, so there is no object-level memory pool mechanism. This means that Python performs a large amount of malloc and free operations during runtime, frequently switching between user state and kernel mentality, which can seriously affect Python's execution efficiency. To speed up the execution of Python, Python introduces a memory pooling mechanism for managing the application and release of small chunks of memory. This is the pymalloc mechanism mentioned earlier.
Second, go to heavy a=[1,2,4,2,4,5,6,5,7,8,9,0]
A = [1,2,4,2,4,5,6,5,7,8,9,0]
News_a = []
For B in a:
If b not in News_a:
News_a.append (b)
Print (news_a)
Iii. What is a lambda function? What good is he?
A lambda function is a function that can receive any number of arguments (including optional arguments) and return a single expression value.
(Note: Lambda functions cannot contain commands, they contain no more than one expression)
Benefits:
1, lambda function is relatively light, that is, still, it is suitable for the need to complete a function, but this function is only used in this one place, even the name is very casual circumstances;
2, anonymous function, generally used to give filter,map such a function-type programming services;
3, as a callback function, passed to some applications, such as message processing
Python allows you to define a small single-line function.
The form of a lambda function is defined as follows:
Labmda parameter: The expression lambda function returns the value of an expression by default.
You can also assign it to a variable. A lambda function can accept any parameter, including optional arguments, but the expression has only one:
>>> g = lambda x, y:x*y
>>> g (3,4)
12
>>> g = lambda x, y=0, z=0:x+y+z
>>> g (1)
1
>>> g (3, 4, 7)
14
You can also use a lambda function directly without assigning it to a variable:
>>> (Lambda x,y=0,z=0:x+y+z) (3,5,6)
14
If your function is very simple and has only one expression and no command, you can consider a lambda function. Otherwise, you can define the function, after all, the function does not have so many restrictions.
Iv. Introduction to the use of the range () function under Python?
Function prototypes: Range (start, end, scan):

Parameter meaning:
Start: Count starts from start. The default is starting from 0. For example, range (5) is equivalent to range (0, 5);

End: Technology ends with end, but does not include end. For example: Range (0, 5) Yes [0, 1, 2, 3, 4] No 5

Scan: The distance between each hop is 1 by default. Example: Range (0, 5) is equivalent to range (0, 5, 1)
What is the difference between,<.*> and <.*?> when matching HTML tags with python?
When a regular expression is repeatedly matched, such as <.*>, the maximum matching value is returned when the program executes the match.
For example:
Import re
s = ' Print (Re.match (' <.*> ', s). Group ())
will return a match and
Import re
s = ' Print (Re.match (' <.*?> ', s). Group ())
will return to the <.*> This match is called greedy match <.*?> called non-greedy match

Python small face question

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.