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

What are the 10 frequently asked questions in Python interviews?

2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What are the 10 questions commonly asked in Python interviews? for this question, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Overview

Python is a very popular programming language. With the development of machine learning, cloud computing and other technologies in recent years, the demand for Python positions is higher and higher. I have collected 10 questions often asked by Python interviewers for your reference.

1. Class inheritance

There is a piece of code like this:

Class A (object): def show (self): print 'base show' class B (A): def show (self): print' derived show' obj = B () obj.show ()

How to call the show method of class A.

The methods are as follows:

Obj.__class__ = Aobj.show ()

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?

Class A (object): def _ _ init__ (self,a,b): self.__a = a self.__b = b def myprint (self): print'axiang, self.__a, 'breadbasket, self.__b A1 (10) a1.myprint () A1 (80)

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

Class A (object): def _ init__ (self, a, b): self.__a = a self.__b = b def myprint (self): print'astatum, self.__a, 'breadbasket, self.__b def _ call__ (self, num): print' call:', num + self.__a

3. New and init

What does the following code output?

Class B (object): def fn (self): print'B fn' def _ init__ (self): print "B INIT" class A (object): def fn (self): print'A fn' def _ new__ (cls,a): print "NEW", an if a > 10: return super (A Cls). _ new__ (cls) return B () def _ init__ (self,a): print "INIT", an A1 = A (5) a1.fn () a2roomA (20) a2.fn ()

Answer:

NEW 5 B INIT B fn NEW 20 INIT 20 A fn

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.

4. Python list and dict generation

What does the following code output?

Ls = [1str 2 for i in ls if i 4] list1 = [i for i in ls if i > 2] print list1 list2 = [iTunes 2 for i in ls if i > 2] print list2 dic1 = {x: Xerox str 2 for x in (2,4,6)} print dic1 dic2 = {x: 'item' + str (Xerox 2) for x in (2,4,6)} print dic2 set1 = {x for x in' hello world' if x not in 'low level'} print set1

Answer:

[3, 4] [6, 8] {2: 4, 4: 16, 6: 36} {2: 'item4', 4:' item16', 6: 'item36'} set ([' hype, 'ringing,' d'])

5. Global and local variables

What does the following code output?

Num = 9 def F1 (): num = 20 def f2 (): print num f2 () f1 () f2 ()

Answer:

9 9

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.

Num = 9 def F1 (): global num num = 20 def f2 (): print num f2 () f1 () f2 () # prints: # 9 # 20

6. Exchange the values of two variables

One line of code exchanges the values of two variables

Astatine 8 baccalaure9

Answer:

(a _ r _ b) = (b _ r _ a)

7. Default method

The code is as follows

Class A (object): def _ init__ (self,a,b): self.a1 = a self.b1 = b print 'init' def mydefault (self): print' default' A1 = A (10) a1.fn1 () a1.fn2 () a1.fn3 ()

Method fn1/fn2/fn3 is not defined, add code, is undefined methods all call mydefault function, the above code should output

Defaultdefaultdefault

Answer:

Class A (object): def _ init__ (self,a,b): self.a1 = a self.b1 = b print 'init' def mydefault (self): print' default' def _ getattr__ (self,name): return self.mydefault A1 = A (10L20) a1.fn1 () a1.fn2 () a1.fn3 ()

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.

Class A (object): def _ init__ (self,a,b): self.a1 = a self.b1 = b print 'init' def mydefault (self,*args): print' default:' + str (args [0]) def _ getattr__ (self,name): print "other fn:" Name return self.mydefault A1 = A (1010) a1.fn1 (33) a1.fn2 ('hello') a1.fn3 (10)

8. Package management

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.

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

_ _ all__ = ['mod1','mod3']

9. Closure

Write a function that receives the integer argument n and returns a function. The function of the function is to multiply the parameter of the function by n and return the result.

Answer:

Def mulby (num): def gn (val): return num * val return gn zw = mulby (7) print (zw (9))

10. Performance

Parsing the following code is slow.

Def strtest1 (num): str='first' for i in range (num): str+= "X" return str

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.

The answers to the 10 questions commonly asked in the Python interview are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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