python面试中被问的最多的10道题

时间:2023-08-09 11:26:07
1 性能: 解析下面代码慢在哪里
def strtest1(num):
str='first'
for i in range(num):
str+="X"
return str
解析:python中str是一个不可变类型,每次迭代,都会生成一个新的str来储存对象,当num特别大的时候,就会创建很多的str,所有对内存的占用也
会特别的大。 2 闭包:写一个函数,接收整数参数n,返回一个函数,函数的功能是把函数的参数和n相乘并把结果返回
def func(n):
def func1(val):
return val * n
return func1 num = func(2)
print(num(3)) 3 包管理:一个包里有三个模块,mod1.py, mod2.py, mod3.py,但使用from demopack import *导入模块时,如何保证只有mod1、mod3被
导入了
增加__init__.py文件,文件内容为
all =['mod1', 'mod3'] 4 类:添加代码
class A: def __init__(self, a, b):
self.a1 = a
self.b1 = bdef mydefault(self):
print('default') a1 = A(10, 20)
a1.fn1()
a1.fn2()
a1.fu3()
要求输出:
default
default
default 修改后代码如下:
class A: def __init__(self, a, b):
self.a1 = a
self.b1 = b def mydefault(self):
print('default') def __getattr__(self, item):
return self.mydefault a1 = A(10, 20)
a1.fn1()
a1.fn2()
a1.fu3()
方法__getattr__只有在调用没有定义的方法时,才会调用它。 以下代码给mydefault加上了参数:
class A: def __init__(self, a, b):
self.a1 = a
self.b1 = b def mydefault(self, *args):
print('default' + str(args[0])) def __getattr__(self, item):
return self.mydefault a1 = A(10, 20)
a1.fn1(10)
a1.fn2('hello')
a1.fu3(20) 5 交换变量:一行代码交换变量
a, b = b, a 6 全局变量和局部变量:下面代码输了什么
num = 9
def f1():
num = 20 def f2():
print(num) f2()
f1()
f2()
答案:
9
9
修改代码:
num = 9
def f1():
global num
num = 20 def f2():
print(num) f2()
f1()
f2()
得到答案:
9
20 7 list和dict:下面代码输出什么
ls = [1,2,3,4]
list1 = [i for i in ls if i>2]
print list1
list2 = [i*2 for i in ls if i>2]
print list2
dic1 = {x: x**2 for x in (2, 4, 6)}
print dic1
dic2 = {x: 'item' + str(x**2) for x in (2, 4, 6)}
print dic2
set1 = {x for x in 'hello world' if x not in 'low level'}
print set1
答案"
[3, 4]
[6, 8]
{2:4, 4:16, 6:36}
{2:'item4', 4:'item16', 6:'item36'}
{'h', 'r', 'd'} 8 new和init:下面代码输出什么
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", a
if a>10:
return super(A, cls).__new__(cls)
return B()
def __init__(self,a):
print "INIT", a
a1 = A(5)
a1.fn()
a2=A(20)
a2.fn()
答案:
先修改上述代码:
class B:
def fn(self):
print('B fn') def __init__(self):
print('B init') class A:
def fn(self):
print('A fn') def __new__(cls, a):
print('New ', a)
if a > 10:
return super(A, cls).__new__(cls)
else:
return B() def __init__(self, a):
print('init ', a) a1 = A(5)
a1.fn()
a2 = A(20)
a2.fn()
答案:
New 5
B init
B fn
New 20
init 20
A fn 9 方法对象:为了让下面代码运行,需要增加什么
class A:
def __init__(self, a, b):
self.__a = a
self.__b = b def myprint(self):
print('a=', self.__a, 'b=', self.__b) a1 = A(10, 20)
a1.myprint()
a1(80)
答案: class A:
def __init__(self, a, b):
self.__a = a
self.__b = b def myprint(self):
print('a=', self.__a, 'b=', self.__b) def __call__(self, *args):
print('call:', args[0] + 20) a1 = A(10, 20)
a1.myprint()
a1(80) 10 继承:如何让obj继承A类的show方法
class A:
def show(self):
print('base show') class B(A):
def show(self):
print('derived show') obj = B()
obj.show()
答案:
class A:
def show(self):
print('base show') class B(A):
def show(self):
print('derived show') obj = B()
obj.__class__ = A
obj.show()