class Person(object):
__count = 0
@classmethod
def how_many(cls):#参数cls传入类本身
return cls.__count;
def __init__(self,name):
self.name=name;
Person.__count+=1;
__count = 0
@classmethod
def how_many(cls):#参数cls传入类本身
return cls.__count;
def __init__(self,name):
self.name=name;
Person.__count+=1;
2017-12-28
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 AttributeError:
print "AttributeError"
__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 AttributeError:
print "AttributeError"
2017-12-28
import math
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
与高票答案一模一样,感觉甚慰
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
与高票答案一模一样,感觉甚慰
2017-12-28
import time
def performance(unit):
def new_fn(f):
def fn(*args,**kw):
start = time.time()
result = f(*args,**kw)
print 'call ' + f.__name__ + '() in '+str(time.time()-start)+unit
return result
return fn
return new_fn
def performance(unit):
def new_fn(f):
def fn(*args,**kw):
start = time.time()
result = f(*args,**kw)
print 'call ' + f.__name__ + '() in '+str(time.time()-start)+unit
return result
return fn
return new_fn
2017-12-28
def performance(f):
def fff(*args):
t1 = time.time()
r = f(*args)
t2 = time.time()
t = t2 - t1
print 'call factorial() in ',t,'s'
return r
return fff
def fff(*args):
t1 = time.time()
r = f(*args)
t2 = time.time()
t = t2 - t1
print 'call factorial() in ',t,'s'
return r
return fff
2017-12-28
@property
def grade(self):
return (self.__score >= 80 and 'A') or (self.__score < 60 and 'C') or 'B'
def grade(self):
return (self.__score >= 80 and 'A') or (self.__score < 60 and 'C') or 'B'
2017-12-28
def calc_prod(lst):
def f(x, y):
return x * y
def lazy_prod():
return reduce(f, lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
用到了刚讲过的reduce(f,list)方法
def f(x, y):
return x * y
def lazy_prod():
return reduce(f, lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
用到了刚讲过的reduce(f,list)方法
2017-12-28
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
print getattr(p,"__score","attributeerror")
def __init__(self, name, score):
self.name = name
self.__score = score
p = Person('Bob', 59)
print p.name
print getattr(p,"__score","attributeerror")
2017-12-28
def cmp_ignore_case(s1, s2):
x = s1[0:].lower()
y = s2[0:].lower()
if x>y:
return 1
if x<y:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
x = s1[0:].lower()
y = s2[0:].lower()
if x>y:
return 1
if x<y:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2017-12-28
我觉的这个好理解多了,把获取到的值 求模 该值的平方根 是否等于 0
import math
def is_sqr(x):
return x % math.sqrt(x) == 0
print filter(is_sqr, range(1, 101))
import math
def is_sqr(x):
return x % math.sqrt(x) == 0
print filter(is_sqr, range(1, 101))
2017-12-28
sorted(iterable, /, *, key=None, reverse=False)
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
2017-12-28
import json
class Students(object):
def read(self):
return r'["Tim","Bob","Alice"]'
s = Students()
print json.load(s)
class Students(object):
def read(self):
return r'["Tim","Bob","Alice"]'
s = Students()
print json.load(s)
2017-12-28
def count():
fs = []
for i in range(1, 4):
def f(i):
return lambda : i*i
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
return lambda : i*i
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-12-28