Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case study of traps in Python interview questions

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "trap case analysis of Python interview questions". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and study the trap case analysis of Python interview questions.

Question 1: how do I modify the following Python code so that the following code calls the show method of class A?

Answer: the test point of this question is class inheritance, as long as you specify the class object through the _ _ class__ method. The supplementary code is as follows:

Question 2: how can I modify the following Python code to make it work?

A: this question examines the method object. In order to enable the object instance to be called directly, you need to implement the _ _ call__ method. The supplementary code is as follows:

Question3: what is the output of the following code?

A:

This question examines the use of new and init. Using the _ _ new__ method, you can decide which object to return, which is called before the object is created, which is common in singletons and factory patterns in design patterns. _ _ init__ is created and the object is called.

Question 4: what does the following code output?

A:

This question examines the generation of lists and dictionaries.

Question 5: what does the following code output?

A:

This question examines global and local variables. 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.

Question 6: how do I exchange two variable values with one line of code?

A:

Question 7: how do I add code so that all undefined methods call mydefault methods?

A:

This question examines the default method of Python, and the method _ _ getattr__ is called only when 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.

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

A: add the _ _ init__.py file to the package and add:

Problem 9: write a function that takes an integer argument n and returns a function that returns the product of n and the parameter.

A:

Question 10: what are the hidden dangers of the following code?

Answer: because the variable str is an immutable object, each iteration, python generates a new str object to store the new string. The larger the num, the more str objects are created and the greater the memory consumption.

Thank you for your reading, the above is the content of "trap case analysis of Python interview questions". After the study of this article, I believe you have a deeper understanding of the trap case analysis of Python interview questions, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report