Some recent interview questions from Python (ii)

Source: Internet
Author: User
Tags iterable

1. Explain what is the stack overflow and under what circumstances it may occur.

Stack overflow is due to the fact that the C language family does not have a built-in check mechanism to ensure that data copied to the buffer is not larger than the buffer size, so when the data is large enough it overflows the buffer range.
In Python, function calls are implemented through a data structure such as stacks (stack), and each time a function call is entered, the stack adds a stack frame, and whenever the function returns, the stack is reduced by a stack frame. Because the size of the stack is not infinite, there are too many recursive calls that can cause the stack to overflow.
The above content from Baidu Encyclopedia. Https://baike.baidu.com/item/%E6%A0%88%E6%BA%A2%E5%87%BA/8538051?fr=aladdin

Several scenarios of stack overflow:

    1. local array too large, When an array inside a function is too large, it is possible to cause a stack overflow.
    2. recursive call hierarchy too many. The recursive function performs a stack operation at run time, and when the stack count is too many times, it can cause a heap overflow.
    3. pointer or array out of bounds. This is the most common scenario, such as copying a string, or handling user input, and so on.
      The above content is from, 22294291/
2. Briefly describe the memory management mechanism of CPython

Python and other advanced programming languages, such as Java, Ruby, or JavaScript, have an automatic memory management mechanism. So many program developers don't pay much attention to memory management, but this can lead to more memory overhead and memory leaks. This article is an in-depth analysis of how CPython (Python interpreter) manages the life cycle of objects, which I recorded in the Vprof project on GitHub, and I hope it will help you.

Reference count

Each Python object has a reference counter----used to record how many other objects are pointing to (referencing) the object. It is stored in the variable refcnt and is implemented by calling the C macro py_incref to increase the reference count and reduce the PY_DECREF implementation of the reference count. Py_decref is more complicated when the counter is referenced to 0 o'clock, it runs the object's deallocation function and reclaims objects of that type.

There are typically two scenarios in which you need to consider this macro definition: Implement your own creation of a data structure, or modify a Python C API that already exists. If you use Python's built-in data structure, then no action is required.

If you want to not increase the reference count, you can use a weak reference or Weakrefs to reference the object. Weakrefs is useful for implementing caching and proxies.

Garbage collection (GC)

Reference counting is the only way to manage the object life cycle before Python 2.0. It has a weakness that it cannot delete objects that are referenced by loops. The simplest example of a circular reference is the object reference itself.


In general, you can avoid using circular referencing objects, but are sometimes unavoidable (for example, long-running programs).

To solve this problem, Python 2.0 introduces a new garbage collection mechanism. The main difference between the new GC and the GC for other language runtimes, such as the JVM and the CLR, is that it is only used to look for circular references that have reference counts.

Circular references can only be created by Container objects, so Python GC does not track types such as integers, strings, and so on.

The GC divides objects into 3 generations, and each generation has a counter and a threshold value. When an object is created, the threshold is automatically assigned to 0, which is the No. 0 generation object. When the counter is larger than a threshold, the GC runs on the current object generation and reclaims the object. Objects that are not reclaimed are moved to the next generation and the corresponding counters are reset. The next generation of objects remains in the next generation.

Before Python 3.4, the GC had a fatal disadvantage----the del__ () method was overloaded with each object, because each object could be referenced to each other, so the GC did not know which __del() method to call that object, which would cause the GC to skip the objects directly. Specific details can refer to gc.garbage and circular references need to be manually broken by the programmer.

Python3.4 introduces a final solution finalization approach, now the GC can break the object's circular reference, instead of using the method described in Gc.garbage to reclaim objects.

Also, it's worth mentioning that if you're sure your code doesn't create a circular reference (or you don't care about memory management), you can rely on reference counters to automatically manage memory instead of using GC to manage memory.

The above content is from:, https://python.freelycode.com/contribution/detail/511
English Original: https://medium.com/@nvdv/cpython-memory-management-479e6cd86c9#.sbvb0py87

3. Please list the Magic methods and uses you know about Python. __init__

constructor, the initialization method that is called when an instance is created

__new__
  1. __new__ is the first method that is called when an object is instantiated
  2. Its first parameter is this class, and the other parameters are used to pass directly to the __init__ method
  3. __new__ decides whether to use the __init__ method, because __new__ can invoke the constructor of another class or return the other instance object directly as an instance of this class, __init__ will not be called if the __new__ does not return an instance object
  4. __NEW__ is primarily used to inherit an immutable type such as a tuple or string
__call__

Allows an instance of a class to be called like a function

__del__

destructor, the method that is called when an instance is destroyed

__len__ (self): Defines the behavior when called by Len ()
__repr__ (self): Defines the behavior when called by Repr ()
__str__ (self): Defines the behavior when called by STR ()
__bytes__ (self): Defines the behavior when called by bytes ()
__hash__ (self): Defines the behavior when a hash () is called
__bool__ (self): Defines the behavior when called by BOOL (), which should return True or False

4. The following list is known:

List1 = [
{
"MM": 2,
},{
"MM": 1,
},{
"MM": 4,
},{
"MM": 3,
},{
"MM": 3,
}
]

4.1 Sort the elements in List1 by the value of MM.

First, the specific use of the function sorted:

(1). Python2.x:sorted (iterable, Cmp=none, Key=none, Reverse=false)
, the python3.x functions of python3.x:sorted (iterable,/, *, Key=none, reverse=false), python2.x and sorted are somewhat different, with fewer CMP parameters.
Key accepts a function that accepts only one element, the default is None
Reverse is a Boolean value. If set to True, list elements are sorted in reverse order and default to False
Focus on how key works:
key specifies a function that receives a parameter that is used to extract a keyword from each element for comparison. The default value is None.

(2). The. Sorted () function sorts all objects that can be iterated.
(3). For a list of tuple types such as

List2=[(' B ',4),(' A ',0),(' C ',2),(' d ',3)]

The sorting method is to use lambda and then get the subscript of the element that needs to be sorted.

print(sorted(list2,key=lambda x:x[0]))

And there's a problem. Inside is the dictionary type so we need to get it through the following ways

Answer:

sorted(list1,key=lambda x:x.items()[0][1])

Or

sorted(list1,key=lambda x:x[‘mm‘]))

or use the operator function to speed up the order, which is equivalent to

 from   operator import   itemgetter 
print (Sorted (List1,key=itemgetter ( ' mm ' ))

4.2 Gets the element with the first mm value equal to x in the List1.
4.3 Remove all elements in list1 that are equal to X and do not reassign the list.
4.4 Remove the List1 the largest element in mm, cannot be sorted.

Subsequent

Some recent interview questions from Python (ii)

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.