1.
以下程序输出结果是什么
a = 1 def fun(a): a = 2 fun(a) print (a)
输出: 1
a = [] def fun(a): a.append(1) fun(a) print(a)
输出: [1]
2.请简要说明什么是类变量,什么是实例变量,并观察以下程序的输出结果
class Person: name="aaa" p1=Person() p2=Person() p1.name="bbb" print(p1.name) print(p2.name) print(Person.name)
在类中,属性是用变量来表示的。类变量=静态变量,特指类中独立于方法之外的变量。实例变量则是在方法中的变量。
bbb
aaa
aaa
3.以下语句有什么弊端,name是元组的时候,程序会是什么样的结果,如何避免
"hi there %s" % name
答:弊端是无法传递一个变量和元组。如果name是一个元组,程序会直接报错,可以将%a替换成.format来避免
4.阅读下面的代码,写出A0,A1至An的最终值。
A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
A1 = range(10)
A2 = [i for i in A1 if i in A0]
A3 = [A0[s] for s in A0]
A4 = [i for i in A1 if i in A3]
A5 = {i:i*i for i in A1}
A6 = [[i,i*i] for i in A1]
A0:{'a':1,'b':2,'c':3,'d':4,'e':5}
A1:range(0,10)
A2:[]
A3:[1,2,3,4,5]
A4:[1,2,3,4,5]
A5:[0:0,1:1,2:4,3:9,4:16,5:25,6:36,7:49,8:64,9:81]
A6:[[0,0],[1,1],[2,4],[3,9],[4,16],[5,25],[6,36],[7,49][8,64],[9,81]]
5.你如何管理不同版本的代码?
答:使用github来管理不同的版本代码,只需将不同版本的代码放到不同的存储库中
6.下面代码会输出什么:
def f(x,l=[]): for i in range(x): l.append(i*i) print(l) f(2) f(3,[3,2,1]) f(3)
f(2): [0,1]
f(3,[3,2,1]): [3,2,1,0,1,4]
f(3): [0,1,4]
7.这两个参数是什么意思:*args
,**kwargs
?我们为什么要使用它们?
答:*args命名关键字参数,而**kwargs:关键字参数
如果我们不确定要往函数中传入多少个参数,或者我们想往函数中以列表和元组的形式传参数时,那就使要用*args。
如果我们不知道要往函数中传入多少个关键词参数,或者想传入字典的值作为关键词参数时,那就要使用**kwargs。
8.阅读下面的代码,它的输出结果是什么?
class A(object): def go(self): print "go A go!" def stop(self): print "stop A stop!" def pause(self): raise Exception("Not Implemented") class B(A): def go(self): super(B, self).go() print "go B go!" class C(A): def go(self): super(C, self).go() print "go C go!" def stop(self): super(C, self).stop() print "stop C stop!" class D(B,C): def go(self): super(D, self).go() print "go D go!" def stop(self): super(D, self).stop() print "stop D stop!" def pause(self): print "wait D wait!" class E(B,C): pass a = A() b = B() c = C() d = D() e = E()
# 说明下列代码的输出结果
a.go()
b.go()
c.go()
d.go()
e.go()
a.stop()
b.stop()
c.stop()
d.stop()
e.stop()
a.pause()
b.pause()
c.pause()
d.pause()
e.pause()