class Person(object):
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count,
p1 = Person('Bob')
p2 = Person('Alice')
#Person.__count
print 'attributeerror'
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count,
p1 = Person('Bob')
p2 = Person('Alice')
#Person.__count
print 'attributeerror'
2017-09-17
你可以认为 -1 0 1 是一根 位置数轴
如果 a > b
把a放到前面
如果 a < b
把a放到后面
def cmp(a, b):
if a > b:
return -1
if a < b:
return 1
return 0
如果 a > b
把a放到前面
如果 a < b
把a放到后面
def cmp(a, b):
if a > b:
return -1
if a < b:
return 1
return 0
2017-09-16
class Fib(object):
def __init__(self, num):
self.table = [0, 1]
for x in range(2, num):
self.table.append(self.table[x-1] + self.table[x-2])
def __str__(self):
return str(self.table)
def __len__(self):
return len(self.table)
def __init__(self, num):
self.table = [0, 1]
for x in range(2, num):
self.table.append(self.table[x-1] + self.table[x-2])
def __str__(self):
return str(self.table)
def __len__(self):
return len(self.table)
2017-09-16
def gcd(a, b):
r = None
while b:
r = a % b
a = b
b = r
return a
r = None
while b:
r = a % b
a = b
b = r
return a
2017-09-16
def count():
fs = []
for i in range(1, 4):
def f(i):
def g():
return i * i
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
def g():
return i * i
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-09-16
def count():
fs = []
for i in range(1, 4):
fs.append(i*i)
return fs
f1, f2, f3 = count()
print f1, f2, f3
完全无需再调用函数嘛
fs = []
for i in range(1, 4):
fs.append(i*i)
return fs
f1, f2, f3 = count()
print f1, f2, f3
完全无需再调用函数嘛
2017-09-16
print(list(filter(lambda x: int(math.sqrt(x)) ** 2 == x, range(1, 100))))
2017-09-16
类只要有read()方法,就称为File-like Object,都可以传给json.load()。例子中read返回的str被json.load解析成了list,也就是json.load内部肯定调用到read,达到我们想要的目的:做解析。File-like Object 都能传给json.load解析,呈现的是多态性,是这样理解吗
2017-09-16