Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of Python interview questions

Shulou Source: shulou.com Published: 2022-06-02 01:54:13 10月03日 Update

This article mainly introduces the relevant knowledge of "case analysis of Python interview questions". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "case analysis of Python interview questions" can help you solve the problem.

I. Class inheritance

Problem: there is a code like this:

How do I call the show method of class A?

Answer: the method is as follows:

The _ _ class__ method points to the class object, just assign it a value of type A, then call the method show, but remember to modify it when you run out of it.

2. Method object

Question: what code needs to be added in order for the following code to run?

Answer: in order for the object instance to be called directly, you need to implement the _ _ call__ method

III. New and init

Question: what is entered in the following code?

Answer:

Using the _ _ new__ method, you can decide which object to return, that is, before the object is created, this can be used in the singleton, factory pattern of the design pattern. _ _ init__ is created and the object is called.

IV. Python list and dict generation

Question: what does the following code output?

Answer:

Global and local variables

Question: what does the following code output?

Answer:

Num is not a global variable, so each function gets its own copy of num, and if you want to modify num, you must declare it with the global keyword. Such as the following.

Exchange the values of two variables

Problem: one line of code exchanges the values of two variables

Answer:

7. Default method

Question: the following code

The method fn1/fn2/fn3 is not defined. Add code so that all undefined methods call the mydefault function. The above code should output

Answer:

The method _ _ getattr__ is called only if there is no defined method call. When the fn1 method passes parameters, we can add a * args indefinite parameter to the mydefault method to be compatible.

8. Package management

Question: there are three modules in a package, mod1.py, mod2.py, mod3.py, but when using from demopack import * to import modules, how to ensure that only mod1 and mod3 are imported.

Answer: add _ _ init__.py file and add:

IX. Closure

Problem: write a function, receive the integer parameter n, return a function, the function of the function is to multiply the parameter of the function and n and return the result.

Answer:

10. Performance

Question: how slow is it to parse the following code

Answer: the str of python is an immutable object. Each iteration, a new str object is generated to store new strings. The larger the num, the more str objects are created and the greater the memory consumption.

This is the end of the content of "case Analysis of Python interview questions". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Method code question answer object function instance parameter instance analysis analysis variable knowledge output two global only file module pattern industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno NVidia Xiaomi Docker