通关密码整理
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count+=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print "attributeerror"
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count+=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except:
print "attributeerror"
2016-09-14
class Fib(object):
def __init__(self, num):
L = [0,1]
for i in range(num-2):
L.append(L[-1]+L[-2])
self.num = L
def __str__(self):
return str(self.num)
def __len__(self):
return len(self.num)
f = Fib(10)
print f
print len(f)
def __init__(self, num):
L = [0,1]
for i in range(num-2):
L.append(L[-1]+L[-2])
self.num = L
def __str__(self):
return str(self.num)
def __len__(self):
return len(self.num)
f = Fib(10)
print f
print len(f)
2016-09-14
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
2016-09-14
def format_name(s):
s.swapcase()
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
s.swapcase()
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-09-14
def count():
fs = []
for i in range(1, 4):
a = (lambda x: lambda: x * x )(i)
fs.append(a)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
a = (lambda x: lambda: x * x )(i)
fs.append(a)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-09-13
print sorted(['bob', 'about', 'Zoo', 'Credit'], lambda x, y: cmp(x.lower(), y.lower()))
2016-09-13