Autumn recruitment algorithm position python interview experience

Table of contents

1.Python’s deep copy and shallow copy

2.Can Python’s multi-threading use multiple CPUs?

3.Python garbage collection mechanism

4. The difference between iterators and generators

5. Decorator

6.Python data types

7.Three ways to delete elements from a python list

8.The difference between python yield and return

9. Hashable objects and unhashable objects in python

10. How to sort a dictionary based on value

11.Python lambda function

12.Magic methods in python

13.What optimizations does python have on memory?

14.The difference between class methods and static methods in python

15. Implementation of multi-threading and multi-process in python

16. Passing by value in Python

17. Monkey patching

18.The difference between is and == in python

19. Commonly used simple writing methods in python

20.The difference between range in python2 and python3

21. The __init__.py file is often seen in the project


1.Python’s deep copy and shallow copy

The object obtained by shallow copy shares a memory address with the original object. When one object changes, the other one will also change. Shallow copies can be completed using slicing, copy.copy(), list(), etc.

Deep copy refers to creating a brand new object that does not interfere with the original object. You can use copy.deepcopy().

2.Can Python’s multi-threading use multiple CPUs?

Because each Python process has a global interpreter lock (GIL), only one thread can occupy this lock globally (including multiple CPUs). This allows each python process to run on only one CPU. Then someone asked, since multi-threading does not work, why should we use it? That is because there is still the problem of thread blocking. For blocking tasks such as intensive I/O operations and network requests, when a thread is blocked , it can release the GIL and allow other threads to continue working, which can be appropriately accelerated, so it is still useful.

So how do we solve the problem of GIL? Multiple processes can be used, each process has its own independent GIL lock and does not affect each other. Use other python interpreters such as PYPY.

For intensive IO operations, multi-threading is generally used, and for intensive computing tasks, multi-processing is generally used. Multiple threads share a memory space, so information transfer is simpler.

3.Python garbage collection mechanism

Python's garbage collection mechanism automatically handles the memory allocation and release of objects. There are generally two recycling mechanisms: one is reference counting. Each object in python has a counter to record the number of references pointing to the object. When it becomes 0, it will be automatically released. There is also a cyclic garbage collection mechanism. Circular reference refers to a group of objects that refer to each other to form a closed loop, and there is no external reference pointing to this closed loop. At this time, the cyclic garbage collection mechanism is used to detect and clean up periodically. When performing garbage collection, first All current objects will be marked, and then those unmarked objects will be recycled. This will clear out cyclic references and other objects that cannot be determined by reference counting.

4. The difference between iterators and generators

Iterators and generators are two different mechanisms in python for working with iterable objects. To learn iterators, you need to manually implement the __iter__ and __next__ methods, and define the iteration logic in next. The generator is a special function. Each time the generator is called, it will save the current execution status and pause at the yield statement. The next time it is called again, execution will continue from where it ended last time until the end of the function. Or encounter the next yield. Generators simplify the creation of iterators and are suitable for lazy calculations. And iterators are suitable for complex iteration logic. Generators save memory because they only retain the current state at runtime rather than generating all values ​​at once.

When a generator function is called, it does not execute immediately, but returns a generator object. Whenever the generator's __next__()method is called, the generator function will continue execution from where it last paused until the next yieldstatement is encountered.

def fibonacci(): //生成器函数
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

# 使用生成器逐个打印斐波那契数列的前十个数
fib = fibonacci()
for _ in range(10):
    print(next(fib))
class MyIterator:
    def __init__(self, data):
        self.data = data
        self.index = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.index >= len(self.data):
            raise StopIteration
        value = self.data[self.index]
        self.index += 1
        return value

# 创建一个列表
my_list = [1, 2, 3, 4, 5]

# 创建一个迭代器对象
my_iter = MyIterator(my_list)

# 使用迭代器遍历列表并打印每个元素
for item in my_iter:
    print(item)

 my_iter is an iterator object, which is traversed using a for loop. The value obtained each time is returned after processing in next.

5. Decorator

A decorator is a special function used to extend, wrap and modify a function without changing its original definition. Usually in the form of function nesting, a function is accepted as a parameter. The definition of decorators is implemented through closures.

def counter(func):
    count = 0

    def wrapper(*args, **kwargs):
        nonlocal count
        count += 1
        print(f"函数 {func.__name__} 已被调用 {count} 次")
        return func(*args, **kwargs)

    return wrapper

# 使用装饰器修饰目标函数
@counter
def say_hello(name):
    print(f"Hello, {name}!")

# 调用被修饰的函数
say_hello("Alice")
say_hello("Bob")

6.Python data types

string

List types (list) are represented by [] brackets.

Tuple type (tuple): represented by parentheses.

Dictionary type (dict): {}, use colons between key values, and use commas between different key value pairs.

Set: {}.

7.Three ways to delete elements from a python list

del deletes the element at the specified position by index, or deletes all.

remove removes the first matching item from the list by value.

pop removes elements by index and returns a value.

my_list = [1, 2, 3, 4, 5]
del my_list[2]  # 删除索引为2的元素
print(my_list)  # 输出:[1, 2, 4, 5]

del my_list  # 删除整个列表

# 注意:使用 del 关键字删除的元素是永久性删除,不可恢复。



my_list = [1, 2, 3, 4, 5]
my_list.remove(3)  # 删除值为3的元素
print(my_list)  # 输出:[1, 2, 4, 5]

# 如果要删除多个匹配项,需要循环调用 remove 方法。


my_list = [1, 2, 3, 4, 5]
value = my_list.pop(2)  # 删除索引为2的元素,并返回该元素的值
print(my_list)  # 输出:[1, 2, 4, 5]
print(value)  # 输出:3

# 如果不指定索引,pop 方法默认删除最后一个元素,并返回其值。

8.The difference between python yield and return

return will end the execution of the function, while yield will return a value and pause the execution of the function, and then remember the execution status of the function so that execution can continue from the pause.

9. Hashable objects and unhashable objects in python

Hashable objects can be used as keys of dictionaries or elements of collections, which contain: numeric types, strings, and tuples. Unhashable objects include: lists, dictionaries, sets

10. How to sort a dictionary based on value

The items() method is required to sort the dictionary. To operate based on values, the lambda function is required.

my_dict = {'apple': 5, 'banana': 2, 'orange': 8, 'kiwi': 3}
my_dict = dict(sorted(my_dict.items(), key = lambda x : x[1]))

11.Python lambda function

A lambda function is an anonymous function that can be used when a simple function is needed. Followed by the parameter list and a colon, the colon is followed by a function expression, and the result of this expression is used as the return value of the lambda.

# 使用 lambda 函数计算两个数的和
sum_func = lambda a, b: a + b

result = sum_func(3, 4)
print(result)

12.Magic methods in python

__new__ method: is a method called before an object is instantiated. It is responsible for creating and returning an instance object. At least one of the parameter lists must be the class itself

__init__ method: It is a method called after an object is instantiated. It is used to initialize the attributes of the instance object. At least one of the parameter lists must be an instance object, usually named self.

__call__ method: allows an object to be called like a function. __call__Methods are triggered when an object is called as a function .

13.What optimizations does python have on memory?

Reference counting and cyclic garbage collection mechanisms. Iterators and generators generate data on demand rather than all at once.

14.The difference between class methods and static methods in python

In Python, class methods and static methods are special methods defined in classes, but they have some differences in functions and usage.

  1. Class method:
    • Use  @classmethod decorators to decorate methods and make them class methods.
    • The first parameter is agreed to  clspoint to the class itself, through which you can access the properties of the class and call other methods of the class.
    • Class methods can be called by the class itself or by instances of the class.
    • Class methods are usually used to perform class-related operations, such as creating objects, modifying class properties, etc.
  2. static method:
    • Use  @staticmethod decorators to decorate methods to make them static.
    • Static methods have no special handling of classes or instances, so there is no default first parameter pointing to the class or instance.
    • Static methods can be accessed through classes or instances.
    • Static methods are typically used to perform functionality related to a class but do not require accessing the properties of the class or calling other methods of the class
  • Both class methods and static methods are special methods of the class, which are modified into specific types of methods through decorators.
  • Class methods can access the properties of the class and call other methods of the class through the first parameter  cls , and are mainly used to perform class-related operations.
  • Static methods have no default class or instance parameters, can be accessed through the class or instance, and are mainly used to perform class-related functions, but do not need to access the properties of the class or call other methods of the class.
    class MyClass:
        @classmethod
        def class_method(cls):
            print("This is a class method")
    
    # 调用类方法
    MyClass.class_method()
    
    
    
    
    class MyClass:
        @staticmethod
        def static_method():
            print("This is a static method")
    
    # 调用静态方法
    MyClass.static_method()

15. Implementation of multi-threading and multi-process in python

import threading

# 定义一个函数作为线程的执行体
def my_thread():
    print("Thread started.")

# 创建 10 个线程
threads = []
for i in range(10):
    thread = threading.Thread(target=my_thread)
    thread.start()
    threads.append(thread)

# 等待所有线程执行完毕
for thread in threads:
    thread.join()

print("All threads are done.")

In the above code, we create a list of threads threadsand then create 10 threads through a loop and add them to the list. Each thread is start()started and begins execution by calling the method. Next, we use another loop to traverse the thread list and call join()the method of each thread. The program will block at this method until the thread completes execution before continuing to execute the next line of code. This ensures that the main thread waits until all child threads have finished before continuing.

import multiprocessing

# 定义一个函数作为进程的执行体
def my_process():
    print("Process started.")

# 创建 10 个进程
processes = []
for i in range(10):
    process = multiprocessing.Process(target=my_process)
    process.start()
    processes.append(process)

# 等待所有进程执行完毕
for process in processes:
    process.join()

print("All processes are done.")

16. Passing by value in Python

Mainly based on whether the parameter is a mutable object or an immutable object. For immutable objects, such as numeric types, strings, and tuples, they are passed by value and will not affect the original parameters.

For mutable objects, such as lists, dictionaries, sets, etc., passing by reference will operate directly on the original object.

17. Monkey patching

It refers to dynamically modifying existing code or class members at runtime.

class MyClass:
    def method(self):
        print("Original method")

# 定义新方法
def new_method(self):
    print("Patched method")

# 将新方法绑定到原始类
MyClass.method = new_method

# 创建对象并调用方法
obj = MyClass()
obj.method()  # 输出: "Patched method"

18.The difference between is and == in python

is determines whether two objects refer to the same address in memory. == is used to determine whether the two values ​​​​are equal.

19. Commonly used simple writing methods in python

List expression [i for i in range(n)]

Dictionary generation (tuple becomes dictionary) {x[0] : x[1] for x in turple}

List slice reverse [::-1]

20.The difference between range in python2 and python3

In python2, the range() function returns a list type, and in python3, it returns an iterator. The next element will only be calculated when needed, which takes up less memory and can be used directly with list(range(n)).

21. The __init__.py file is often seen in the project

It allows the current directory to be treated as a package and provides the ability to initialize and control import behavior. It is an important part of building and organizing complex projects. __init__.pyFiles can also __all__control the import behavior of packages by defining variables. __all__Is a list that specifies from package import *which submodules or objects should be imported when using the statement to import a package

Guess you like

Origin blog.csdn.net/slamer111/article/details/131541402