python interview questions - primary (II)

Without a solid foundation, the earth was moving, the interview often asked some of the usually very easy to ignore the knowledge base, so focusing on accumulation, see more in-depth understanding back to work suddenly dawned on a particular day .

In order to face questions not only deal with the interview, it is a sort of categorize and summarize knowledge overstating, the so-called can not make bricks without straw, under a multi-Tuen winter grain, ground training, an effort to gain.

The most important place ahead:

☆ ♠ ☆ ♠ ☆ ♠ how Python is memory management ☆ ♠ ☆ ♠ ☆ ♠

Long, first remember understanding ------------------------------------------ ------

First, the garbage collection:

    python Unlike C ++, and other languages, like Java, they can not declare variables in advance the type of direct variable assignment. Python in terms of language, object type at run time and memory are determined. This is why we call python language is dynamically typed reasons (here we dynamically typed languages ​​can simply be attributed to a variable allocation of memory addresses at run time automatically determine the variable type and variable assignment).

Second, the reference count:

    python using the same windows kernel objects similar way to manage the memory. Each object, which maintains a count of pointing the object reference. When the variable is bound to an object, the variable reference count is 1, (and in other cases will lead to increased count variable references), the system will automatically maintain these tags, and the timing of the scanning, when a label when the reference count becomes 0, the object will be recovered.

Third, the memory pool mechanism

    python memory mechanism into a pyramid:

    -1, -2 main operating system layer operation;

    Layer 0 is the C malloc, free and other memory allocation and deallocation function operation;

    The first layer and the second layer is a memory pool, the python has an interface function PyMem_Malloc function implemented when the object of this layer is less than 256 bytes of memory directly assigned;

    The third layer is the uppermost layer, that is, we direct the operation of the python object;

When the C malloc and free calls if frequent, will produce performance problems, coupled with frequent memory allocation and release small pieces of debris will produce memory.

python here doing the work in the main are:

If the request allocation of memory between 1 and 256 bytes on the use of their own memory management system, or directly using malloc.

Here still calls malloc to allocate memory, but every time he returns to allocate a chunk size of 256 bytes of memory.

 

Registration via the memory pool memory to eventually be recycled to the memory pool, and does not call the free C relieved, for the next use. Python for simple objects, such as the value, a string, a tuple (tuple is not allowed to be changed) is used in a duplicated manner (deep copy), that is, when speaking to another variable B is assigned to the variable A, while A and B's memory space remains the same, but when the value of a changes, re-allocation of space to the a, a and B address is no longer the same.

 

A, Python role in yield:

yield is simply a generator so that the function it remembers position in the function to return the last time . For the second (or n times) to generate the function call jump.

Second, how to improve the operating efficiency of python

1 using the generator; 2 the key code using the external function package (Cython, pylnlne, pypy, pyrex); 3 for the optimization cycle - to avoid access attribute variables in the circulation.

Third, the online service may hang because of various reasons how to do?

Background process in Linux management tool supervisor

After each files modified to perform service supervisord restart in linux

Four, find and grep difference:

grep command is a powerful text search tools, grep all content strings can be regular expressions, allowing text file lookup mode. If a matching pattern is found, grep prints all lines containing pattern.

find commonly used to search for eligible files in a particular directory, it can also be used to search for specific user owner of the file.

Fifth, the decorator of the role and functions:

  • The introduction of the log
  • Function execution time statistics
  • Execution of the function of money preparatory process
  • Cleaning function after the function is executed
  • Check permissions and other scenes
  • Cache

Sixth, the file size of data read:

  1.  Generator generator using
  2. Iterator iterates over: for line in file

Seven, iterators and generators difference:

  1. An iterator is a more abstract concept, any object, if it is next class has its own method and the method returns iter. For this type of container object string, list, dict, tuple, etc., used for loop iterates it is convenient. In the background for statement calls iter () function, iter () on the container object is Python's built-in functions. ITER () returns a defined next () method of the iterator object, which individually accessed within the container elements in the container, built next () function is the python. In the absence of subsequent elements, next () will throw an exception of a StopIterration.
  2. Generator (Generator) is a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement at the time required to return the data. Each time next () is called, the generator will return to the position (its disengaged position it remembers the last time the statement is executed and all data values )

    Differences: Builder can do all things iterators can do, but also created automatically __iter __ () and next () method, the generator is particularly simple, but also highly efficient generator, use a generator expression instead list comprehension can save memory at the same time. In addition to creating and maintaining a state of the program is automatically generated when generators terminate, it automatically ran StopIterration exception.

Eight simple talk about the next GIL:

Global Interpreter Lock (Global Interpreter Lock)

Python Python code executed by a virtual machine (also known as the interpreter main loop, CPython version) to control, Python was designed with consideration to be in the main loop of the interpreter, only one thread in execution, that is, any time, only one thread running in the interpreter. Access to the Python virtual machine is controlled by the Global Interpreter Lock (GIL), it is this lock ensures that only one thread is running .

In a multithreaded environment, Python virtual machine executes the following manner:

1. Set GIL

2. Switch to a thread to run

3. Run:

    a. Specify number of bytecode instructions, or

    b. Thread initiative to the control (you can call time.sleep (0))

4. Put the thread to sleep

5. Unlock GIL

6. All of the above steps again repeated

Then call external code (such as C / C ++ extension functions) when, GIL say will be locked until the end of the function (because there is no Python bytecode is run during this time, so do not thread switching).

 Nine, describing the difference between arrays, linked lists, queues, stacks of?

And a linked list arrays of data storage concept, the array in a continuous spatial stored data, and the list may be non-contiguous space storing data;

Stack and queue describing the data access mode concept queues are FIFO , and the stack is a LIFO ; stack and queue can be implemented with an array, linked list implementation can also be used.

Ten, Python common standard libraries and third-party libraries:

===== ====== third-party libraries: 

  • Scrapy Fast / advanced web crawler frame
  • Numpy scientific computing and mathematical basis for the work package, including statistics, linear algebra, matrix math, financial operations, and so on.
  • Pillow image "painless" processing library, easy to use version of the PIL.
  • Pygame a highly portable game development module.
  • Gooey a command, the command-line program into a GUI program.
  • Apache Libcloud a Python library for a variety of cloud design, through a single, consistent and unified API to access various cloud providers.
  • PyFilesystem Pythonic a generic interface to all file systems provide.
  • Cypthon Python extension tool of the C language, using the type C mixed to Python compiled module to obtain performance improvements.
  • Behold a powerful support print-style debugging tools.
  • Etc. - " https://blog.csdn.net/qq_18941425/article/details/79732712 "

===== ====== standard library: 

  • re: regular matching threading and multi-threaded multiprocessing
  • os / sys system, environment-related
  • cvs
  • Queue Queue
  • math: math
  • datetime: date and time of treatment
  • logging log
  • pdb debugging
  • traceback debugging
  • pprint beautiful output threading and multiprocessing multi-threaded url / urllib2 / httplibhttp library, httplib underlying point, the recommended third-party libraries requests
  • pickle / cPickle tool sequence
  • hashlib md5, sha hash algorithm, etc.
  • json / simplejson python's json library and according to the discussion on the benchmark so, simplejson performance than json
  • timeit run time code or the like is calculated
  • datetime: date and time of treatment
  • performance measurement module cProfile python
  • glob similar listfile, it can be used to find files
  • There is a registered atexit function can be used just to execute some code before running the script exits
  • DIS python disassembly, when a statement is not to be understood that the principles can be used to view the function dis.dis python interpreter instructions corresponding to the code and the like.

 

Practice makes perfect. 

==========================================================

There is a Road, learning is endless, slowly fill it, come on!

 

Guess you like

Origin www.cnblogs.com/pythonbetter/p/11961150.html