1. How do I manage memory in Python?
Memory management in Python is managed by Python private heap space. The object and data structure are located in the private heap, and the developer has no right to access the private heap, which is handled by the Python interpreter. The heap space allocation of Python objects is completed by the memory manager. The core API provides some tools for developers to write code. Python's built-in garbage collector recycles all unused memory, making it suitable for heap space.
2. Explain the Help() function and Dir() function in Python.
The Help() function is a built-in function, which is used to view functions and specify module usage.
*Image source network
The running result is:
*Image source network
Dir() function is a Python built-in function. When Dir() function does not take parameters, it returns the list of variables, methods and defined types in the current range; When there are parameters, the list of properties and methods of the parameter is returned.
Give an example to show how to use it:
*Image source network
The running result is:
*Image source network
3. When Python exits, will all allocated memory be cleared?
The answer is no. When Python exits, Python modules with circular references to other objects and objects referenced from the global namespace will not be deallocated or released. Unable to deallocate those parts of memory reserved by the C library. When exiting, Python will attempt to deallocate/destroy all other objects due to its own efficient cleaning mechanism.
4. What is a monkey patch?
Dynamically modify a class or module during runtime.
*Image source network
The running result is:
*Image source network
5. What is a dictionary in Python?
Dictionaries refer to built-in data types in Python. It defines a one-to-one relationship between keys and values, including a pair of keys and their corresponding values. The dictionary is indexed by a key.
6. Explain the logical operators in Python.
There are three logical operators in Python: and, or, not.
7. Why not suggest an underscore as the beginning of an identifier?
Python has no concept of private variable, so it is agreed to declare a variable as private by starting with a quick underscore. Do not start with an underscore if you do not want the variable to be private.
8. What is Flask?
Flask is a lightweight Web application framework written in Python. The WSGI toolkit uses Werkzeug, and the template engine uses Jinja2. Flask uses BSD authorization. Werkzeug and Jinja2 are two environment dependencies. Flask does not need to rely on external libraries.
9. Explain the join () and split () functions in Python.
Join() can be used to add specified characters to the string.
*Image source network
The running result is:
*Image source network
Split() can be used to specify character splitting strings.
*Image source network
The running result is:
*Image source network
10. How long is the identifier in Python?
The identifier can be of any length. The following rules must also be observed when naming identifiers:
L Can only begin with an underscore or a letter in A-Z/a-z
L The rest can use A-Z/a-z/0-9
L Case sensitive
L Keyword cannot be used as identifier
11. Is indenting required in Python?
need. Python specifies a code block. All codes in loops, classes, functions, etc. are specified in indented blocks. This is usually done using four space characters. If the developer's code is not indented, Python will not execute accurately and will also throw an error.
12. Please explain the meaning of using * args.
When we don't know how many parameters to pass to the function, such as a list or tuple, we use * args.
*Image source network
The running result is:
*Image source network
13. What is the difference between a deep copy and a shallow copy?
Shallow copy is to copy the reference of an object to another object. If changes are made in the copy, the original object will be affected. Deep copy is to copy an object to another object. If you change the copy of an object, the original object will not be affected.
14. How to implement multithreading in Python?
Python is a multithreading language with a built-in multithreading toolkit. Multithreading allows us to execute multiple threads at once. The GIL (global interpreter lock) in Python ensures that a single thread is executed at once. A thread saves GIL and performs some operations before passing it to the next thread, which looks like the illusion of parallel operation. In fact, threads run on the CPU in turn. All transfers will increase the memory pressure of program execution.
15. What are closures in Python?
When a nested function references a value in its outer region, the nested function is a closure. This means that this value will be recorded.
For example:
*Image source network
The running result is:
*Image source network