class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = 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 = Person.__count+1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print "attributeerror"
2017-12-19
还不懂的看着个吧,一定会看懂的。http://www.jb51.net/article/86383.htm
2017-12-19
def count():
fs = []
for i in range(1, 4):
f = lambda x: lambda : x*x
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
f = lambda x: lambda : x*x
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2017-12-18
def calc_prod(lst):
return lambda : reduce( lambda x,y:x*y , lst ,1)
f = calc_prod([1, 2, 3, 4])
print f()
return lambda : reduce( lambda x,y:x*y , lst ,1)
f = calc_prod([1, 2, 3, 4])
print f()
2017-12-18
import math
def is_sqr(x):
if math.modf(math.sqrt(x))[0]>0:
return False
else :
return True
print filter(is_sqr, range(1, 101))
浮点数用==判断你存在精度问题
def is_sqr(x):
if math.modf(math.sqrt(x))[0]>0:
return False
else :
return True
print filter(is_sqr, range(1, 101))
浮点数用==判断你存在精度问题
2017-12-18
def performance(unit):
def performan_decorator(f):
def wrapper(*args, **kw):
t = time.time()
print 'call %s() in %s %s' % (f.__name__, t, unit)
return f(*args, **kw)
return wrapper
return performan_decorator
def performan_decorator(f):
def wrapper(*args, **kw):
t = time.time()
print 'call %s() in %s %s' % (f.__name__, t, unit)
return f(*args, **kw)
return wrapper
return performan_decorator
2017-12-18
import math
def is_sqr(x):
return x%math.sqrt(x) == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x%math.sqrt(x) == 0
print filter(is_sqr, range(1, 101))
2017-12-18
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2017-12-18
def performance(f):
def fn(*args, **kw):
print "call " + f.__name__ + "() in",time.localtime(time.time())
return f(*args, **kw)
return fn
def fn(*args, **kw):
print "call " + f.__name__ + "() in",time.localtime(time.time())
return f(*args, **kw)
return fn
2017-12-18
class BStudent(BasketballMixin, Student):
pass
class FTeacher(FootballMixin, Teacher):
pass
s = BStudent()
print s.skill()
t = FTeacher()
print t.skill()
这样就行了。。。。。。。
pass
class FTeacher(FootballMixin, Teacher):
pass
s = BStudent()
print s.skill()
t = FTeacher()
print t.skill()
这样就行了。。。。。。。
2017-12-17
class BStudent(BasketballMixin, Student):
def __init__(self):
super(BasketballMixin, self).__init__()
super(Student, self).__init__()
class FTeacher(FootballMixin, Teacher):
def __init__(self):
super(FootballMixin, self).__init__()
super(Teacher, self).__init_
def __init__(self):
super(BasketballMixin, self).__init__()
super(Student, self).__init__()
class FTeacher(FootballMixin, Teacher):
def __init__(self):
super(FootballMixin, self).__init__()
super(Teacher, self).__init_
2017-12-17