面试题之题目汇总
第一题和答案
""" def func(a1,a2=[]): a2.append(a1) print(a2) func(1) # [1,4] [1,] func(3,[]) # [3,] [3,] func(4) # [1,4] [1,4] """ """ def func(a1,a2=[]): a2.append(a1) return a2 l1 = func(1) print(l1) # [1,] l2 = func(3,[]) print(l2) # [3, ] l3 = func(4) print(l3) # [1,4] """ """ def func(a1,a2=[]): a2.append(a1) return a2 l1 = func(1) # l1=[1,4] l2 = func(3,[]) # l2=[3,] l3 = func(4) # l3=[1,4] print(l2) print(l1) print(l3) """
第二题和答案
val = [lambda x:x+i for i in range(10)] print(val) #这是一个列表生成式,所以得到的值是这些 #[<function <listcomp>.<lambda> at 0x000001DD09C246A8>, # <function <listcomp>.<lambda> at 0x000001DD09C24730>, # <function <listcomp>.<lambda> at 0x000001DD09C247B8>, # <function <listcomp>.<lambda> at 0x000001DD09C24840>, # <function <listcomp>.<lambda> at 0x000001DD09C248C8>,......] print(val[4]) #得到的是这个列表中的第5个式子<function <listcomp>.<lambda> at 0x000001DD09C248C8> print(val[4](1)) #10 类表中的每个值都是一个函数,func(1)类似于这样,给函数传入了一个1 返回 1+i #因为for循环之后 每个i的值都会变成 9 所以得到的值为10
多态??
# class A: # def send(self): # pass # class B: # def send(self): # pass # # def func(arg): # arg.send() # # obj = B() # func(obj)
继承 就是按照 __mro__的順序去继承
class Foo(object): def execute(self): super(Foo,self).func() class Bar(object): def func(self): print("Bar.func") class CC(Foo,Bar): pass obj1 = CC() # CC.__mro__ obj1.execute() #Bar.func 这是运行后得到的值,因为 CC.__mro__ 是按照这个顺序继承的, # 所以 当执行到super(Foo,self).func() 时候会去Bar中寻找