Python Basics Interview Part 4

1. What are the commonly used libraries in Python and what are their functions?

  1. requests:  requests is a library for sending HTTP requests. It provides a simple and elegant API that can easily send GET, POST, PUT, DELETE and other requests and process response data. It supports common HTTP functions such as session management, authentication, file upload, etc., and is a common choice for web development and API calls.

  2. math:  math is one of Python's standard libraries, providing many functions and constants related to mathematical operations. It includes basic mathematical operations (such as trigonometric functions, exponential functions, logarithmic functions), mathematical constants (such as π and the base e of natural logarithms), mathematical operations (such as rounding, absolute value, power operations), etc. math Libraries are the basis for mathematical and scientific calculations.

  3. websocket:  websocket is a library for creating and managing WebSocket connections. WebSocket is a protocol that enables full-duplex communication between the client and the server. It allows the server to actively push data to the client without the client sending a request. websocket The library provides functionality for creating WebSocket servers and clients, making it easier to implement real-time communications in Web applications.

  4. time:  time is one of Python's standard libraries, providing time-related functions. It includes functions such as obtaining the current time, date and time conversion, timestamp operations, sleep (delayed execution), etc. time The library is widely used in applications that need to process time and date, such as scheduled tasks, logging, performance analysis, etc.

  5. pymysql:  pymysql is a library used to connect and operate MySQL database. It is a popular database interface for Python, providing functions such as executing SQL statements, connecting to databases, and transaction management in Python. pymysql Libraries can be used for many database-related tasks, such as data query, data insertion, data update, etc.

2. Garbage collection mechanism in Python

    In Python, garbage collection (Garbage Collection) is the process of automatically managing memory. It is responsible for detecting and recycling the memory space occupied by objects that are no longer used. Python uses a garbage collection mechanism called reference counting (Reference Counting), and an optional cycle garbage collector (Cycle Detector) to deal with circularly referenced objects.

Reference counting:
Every object in Python has a reference counter that records how many references are currently pointing to the object. When an object is referenced, its reference count is incremented; when a reference is deleted or goes out of scope, the reference count is decremented. When the reference count of an object becomes 0, it means that the object is not referenced, that is, it becomes a garbage object and can be recycled by the garbage collection mechanism.

The advantage of the reference counting mechanism is high real-time performance. When the object is no longer referenced, the memory can be reclaimed immediately. But it also has some shortcomings, such as the inability to handle circular references.

Circular garbage collector:
In order to solve the problem of circular references, a circular garbage collector was introduced in Python. The cyclic garbage collector periodically detects the reference relationships between objects, finds circular reference objects that are no longer referenced, and recycles them. Here's how it works:

  1. Marking Phase: Starting from the root object (such as global variables, activity stack, call stack, etc.), it marks all accessible objects by traversing the reference relationships between objects.
  2. Sweeping Phase: Traverse the entire heap memory, clear unmarked objects, and reclaim the memory space they occupy.
  3. Compacting Phase: Organize the heap memory and move surviving objects to one end to release continuous memory space.

A circular garbage collector can solve circular reference problems that reference counting cannot handle, but it increases the overhead of garbage collection and may cause some pauses while garbage is collected.

Other optimization techniques:
In addition to reference counting and circular garbage collectors, Python also uses some other optimization techniques to improve the performance of garbage collection, such as:

  1. Generational Collection: Divide objects into different generations based on their survival time and adopt different recycling strategies. Most objects tend to become garbage quickly, while only a few survive longer. By using different collection frequencies for different generations, you can reduce the overhead of garbage collection.
  2. Incremental Collection: Breaks the garbage collection process into multiple stages, allowing the program to continue executing between each stage. This way the garbage can be

 3. Understanding objects and references in python

    In Python, objects are abstract representations of data, which can be numbers, strings, lists, functions, etc. Objects occupy a certain amount of space in memory and contain data and methods for operating data. Each object has a unique identity (Identity), which can be id()obtained through built-in functions.

A reference is an identifier pointing to an object, which can be thought of as a pointer or an alias. In Python, references are created through variable names, elements in data structures, function parameters, etc. References allow us to access and manipulate objects, but they do not directly store the object itself, but point to the memory address where the object is located.

Below we discuss some features of objects and references in Python in detail:

1. Object creation and destruction:
Object creation is achieved by using the corresponding constructor or literal value. For example, use str()constructors to create string objects, use []symbols to create list objects, etc. The destruction of objects is automatically handled by the garbage collection mechanism. When the object is no longer referenced, the garbage collection mechanism will reclaim the memory space it occupies.

2. Identity of the object:
Each object is assigned a unique identity when it is created, which can be id()obtained through functions. The identity of an object remains unchanged during its lifetime, even if the object's value changes, its identity does not change.

3. Mutable objects and immutable objects:
In Python, objects can be divided into mutable (Mutable) objects and immutable (Immutable) objects. The value of a mutable object can be modified, while the value of an immutable object cannot be changed. For example, a list (list) is a mutable object, and its value can be changed by modifying its elements; while a string (str) is an immutable object, and once created, its value cannot be modified.

4. Assignment and transfer of references:
In Python, references can be created and modified through assignment operations. When we assign an object to a variable, we actually assign a reference to the object to the variable. This means that there is an association between the variable and the object, but it does not mean that the variable and the object are the same entity.

When calling a function, parameters are passed by reference. When we pass an object as a parameter to a function, the parameter inside the function will refer to the object. This means that modifications to parameters inside the function may affect the original object.

5. The impact of reference counting:
Reference counting is the core of Python’s garbage collection mechanism. Every object has a reference counter that records how many references point to the object. When the reference count becomes 0, it means that the object is no longer referenced and can be recycled by the garbage collector. The advantage of the reference counting mechanism is that it has high real-time performance and can promptly recycle objects that are no longer used, but it cannot handle circular references.

To sum up, objects in Python are abstract representations of data, and objects are accessed and manipulated through references. A reference provides an alias or pointer to an object, allowing us to use the object in a program. At the same time, the garbage collection mechanism manages and recycles no longer used objects through reference counting and cyclic garbage collectors to ensure effective utilization of memory.

4. Implementation of singleton pattern in Python

    In Python, the singleton pattern is a design pattern used to ensure that a class has only one instance and provides a global access point to obtain that instance. The singleton pattern is usually used when sharing resources or global state is required to avoid resource waste or inconsistent state caused by creating multiple instances.

The following is a common implementation of the Python singleton pattern:

1

2

3

4

5

6

7

class Singleton:

    _instance = None

    def __new__(cls*args, **kwargs):

        if not cls._instance:

            cls._instance = super().__new__(cls)

        return cls._instance

In this implementation, class variables are used  _instance to hold unique instances. In  __new__ the method, determine  _instance whether the instance already exists. If it does not exist, create a new instance and assign it to  _instance. If  _instance an instance already exists, the instance is returned directly.

The following is an example to illustrate how to use this singleton class:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

class Logger(Singleton):

    def __init__(self):

        self.log = []

    def add_log(self, message):

        self.log.append(message)

    def print_log(self):

        for message in self.log:

            print(message)

# 创建多个 Logger 实例

logger1 = Logger()

logger2 = Logger()

# logger1 和 logger2 是同一个实例

print(logger1 is logger2)  # 输出: True

# 向 logger1 添加日志

logger1.add_log('Log message 1')

logger1.add_log('Log message 2')

# logger2 也能访问到 logger1 添加的日志

logger2.print_log()

# 输出:

# Log message 1

# Log message 2

5. Commonly used expressions in Python:

    1. Match numbers: \dThis
      expression can match any numeric character. For example, "\d" can match numeric characters such as "1", "2", and "3" in the string.

    2. Match letters: [a-zA-Z]
      This expression can match any alphabetic character. "[a-zA-Z]" matches both uppercase and lowercase letters in a string.

    3. Match whitespace characters: \s
      This expression can match any whitespace character, including space, tab, and newline characters.

    4. Match word boundaries:\bThis
      expression can match a word boundary. For example, "\bword\b" matches the individual word "word" in the string.

    5. Match repeated characters: +
      This expression can match one or more repetitions of the previous character or expression. For example, "a+" matches one or more consecutive letters "a".

    6. Match any character:
      This expression can match any character except newline characters.

    7. Match starting position: ^
      This expression can match the starting position of the string. For example, "^Hello" matches a string that begins with "Hello".

    8. Match end position: $
      This expression can match the end position of the string. For example, "world$" matches a string that ends with "world".

6. In Python, what is a context manager? How to implement a context manager? 

  In Python, the context manager (Context Manager) is a mechanism for managing resources to ensure that resources are acquired and released correctly before and after a code block is executed. Context managers are often  with used with statements to ensure proper opening and closing of resources and proper cleanup even in the event of exceptions.

To implement a context manager, you need to define a class and implement two special methods in the class: __enter__() and  __exit__().

  1. __enter__() Method: This method is called before entering the code block and returns a value that will be  as received by the variable after the statement. Resource acquisition and initialization operations are usually performed in this method.

  2. __exit__() Method: This method is called after the execution of the code block is completed, regardless of whether an exception occurs. It is responsible for resource release and cleanup operations. __exit__() The method receives three parameters: exception type, exception value, and tracing information. If the code block completes normally, these parameters are  None. If an exception occurs, you can  __exit__() handle the exception in the method and return  Trueto indicate that the exception has been handled. If  False a new exception is returned or raised, the exception will be propagated upward.

Here is a simple example showing how to implement a context manager:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

class MyContextManager:

    def __enter__(self):

        # 在进入代码块之前进行资源的获取和初始化

        print("Entering the context")

        return self  # 可选择性地返回一个值给 as 语句后的变量

    def __exit__(self, exc_type, exc_val, exc_tb):

        # 在代码块执行完成后进行资源的释放和清理

        print("Exiting the context")

        if exc_type is not None:

            # 处理异常并返回 True 表示异常已被处理

            print(f"Exception occurred: {exc_type}, {exc_val}")

            return True

# 使用上下文管理器

with MyContextManager() as cm:

    # 在这里执行需要进行资源管理的代码块

    print("Inside the context")

    # 可以在代码块中引发异常来验证异常处理

    # raise ValueError("Something went wrong")

7. Asynchronous programming in Python, including the use of async/await keywords and asyncio module

In Python, asynchronous programming is a programming pattern for writing efficient non-blocking concurrent code. It allows a program to continue performing other tasks while waiting for certain operations to complete without blocking the entire program's execution flow.

The key parts of asynchronous programming are the async/await keywords and the asyncio module.

  1. async/await keyword:

    • async: used to define an asynchronous function. Asynchronous functions can contain  await keywords that pause the execution of the function while waiting for some asynchronous operation to complete.
    • await: Used to wait for the completion of an asynchronous operation. It can be used inside an asynchronous function to pause the execution of the function and allow other tasks to execute until the asynchronous operation completes and returns the result.
  2. asyncio module:

    • asyncio is a module in the Python standard library for asynchronous programming. It provides a set of tools and functions for writing asynchronous code.
    • The main component is the event loop, which is responsible for scheduling and executing asynchronous tasks. The event loop allows multiple tasks to execute concurrently and to suspend and resume task execution when needed.
    • asyncio also provides some helper functions and classes for handling asynchronous operations, such as asynchronous I/O operations, timers, etc.
    • By using the asyncio module, you can write asynchronous code based on callbacks, coroutines, and tasks.

Here is a simple example showing how to use async/await and asyncio for asynchronous programming:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

import asyncio

# 定义一个异步函数

async def greet(name):

    print(f"Hello, {name}!")

    await asyncio.sleep(1)  # 模拟一个耗时的异步操作

    print(f"Goodbye, {name}!")

# 创建一个事件循环

loop = asyncio.get_event_loop()

# 调用异步函数

tasks = [greet("Alice"), greet("Bob")]

# 将异步函数包装成任务对象

# 可以使用 asyncio.create_task() 或 loop.create_task() 创建任务

# create_task() 是 Python 3.7 之后的新语法,推荐使用

# tasks = [asyncio.create_task(greet("Alice")), asyncio.create_task(greet("Bob"))]

# 执行任务并等待完成

loop.run_until_complete(asyncio.wait(tasks))

# 关闭事件循环

loop.close()

In the above example, greet() it is an asynchronous function,  async defined using the keyword. Inside the function, use  await the keyword wait  asyncio.sleep(1) for completion. Create task objects and use event loop  run_until_complete() methods to perform these tasks. Finally, the event loop is closed by calling  loop.close() .

Asynchronous programming is particularly effective when processing I/O-intensive tasks (such as network requests, database access, etc.), because the CPU can be fully utilized to complete other tasks while waiting for a response, thus improving the performance and responsiveness of the program.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/135221431