class Person(object):
__count = 0
def __init__(self,name):
self.name = name
Person.__count = Person.__count + 1
@classmethod
def how_many(cls):
return cls.__count
__count = 0
def __init__(self,name):
self.name = name
Person.__count = Person.__count + 1
@classmethod
def how_many(cls):
return cls.__count
2018-03-26
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >= 80:
print('A-优秀')
elif self.__score >= 60:
print('B-及格')
else:
print('C-不及格')
def __init__(self, name, score):
self.name = name
self.__score = score
def get_grade(self):
if self.__score >= 80:
print('A-优秀')
elif self.__score >= 60:
print('B-及格')
else:
print('C-不及格')
2018-03-26
import math
def is_sqr(x):
return str(math.sqrt(x)).split('.')[1]=='0'
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return str(math.sqrt(x)).split('.')[1]=='0'
print filter(is_sqr, range(1, 101))
2018-03-26
class Person(object):
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
setattr(self, kw.items()[0][0], kw.items()[0][1])
参考:http://www.runoob.com/python/python-func-setattr.html
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
setattr(self, kw.items()[0][0], kw.items()[0][1])
参考:http://www.runoob.com/python/python-func-setattr.html
2018-03-26
def calc_prod(lst):
def xx():
return reduce(lambda x,y:x*y, lst)
return xx
f = calc_prod([1, 2, 3, 4])
print f()
def xx():
return reduce(lambda x,y:x*y, lst)
return xx
f = calc_prod([1, 2, 3, 4])
print f()
2018-03-25
#非递归实现
def gcd(a,b):
while b:
a,b = b,a%b
return a
#递归实现
def gcd(a,b):
return a if b==0 else gcd(b,a%b)
这两个的性能是一样,计算次数是一样的。
一般来说递归慢, 是例如 斐波那契数列这样 有多个数字都需要递归求,会导致相同计算执行多次,才会性能低。
但是递归有个缺点, 递归次数受 执行器 配置的栈长度有关, 如果大于了 栈长度, 会有 溢出错误
def gcd(a,b):
while b:
a,b = b,a%b
return a
#递归实现
def gcd(a,b):
return a if b==0 else gcd(b,a%b)
这两个的性能是一样,计算次数是一样的。
一般来说递归慢, 是例如 斐波那契数列这样 有多个数字都需要递归求,会导致相同计算执行多次,才会性能低。
但是递归有个缺点, 递归次数受 执行器 配置的栈长度有关, 如果大于了 栈长度, 会有 溢出错误
2018-03-25
def _get_gcd(self, a, b):
while True:
q, r = a / b, a % b
if r is 0:
break # b
a, b = b, r
return b
while True:
q, r = a / b, a % b
if r is 0:
break # b
a, b = b, r
return b
2018-03-24
import json
class Students(object):
def __init__(self):
pass
def read(self):
return r'["Tim","Bob","Alice"]'
s = Students()
print json.load(s)
class Students(object):
def __init__(self):
pass
def read(self):
return r'["Tim","Bob","Alice"]'
s = Students()
print json.load(s)
2018-03-24
class Person(object):
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
setattr(self, kw.items()[0][0], kw.items()[0][1])
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
setattr(self, kw.items()[0][0], kw.items()[0][1])
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
2018-03-23