import datetime
def performance(func):
def wrapper(n):
start = datetime.datetime.now()
ret = func(n)
end = datetime.datetime.now()
time = end - start
print('cost time is : %f' % time.total_seconds())
return ret
return wrapper
def performance(func):
def wrapper(n):
start = datetime.datetime.now()
ret = func(n)
end = datetime.datetime.now()
time = end - start
print('cost time is : %f' % time.total_seconds())
return ret
return wrapper
2018-05-31
from os.path import isdir, isfile
print isdir(r'/data/webroot/resource/python') if isdir(r'/data/webroot/resource/python') == True else True
print isfile(r'/data/webroot/resource/python/test.txt') if isdir(r'/data/webroot/resource/python') == True else True
总是 False 受不了
print isdir(r'/data/webroot/resource/python') if isdir(r'/data/webroot/resource/python') == True else True
print isfile(r'/data/webroot/resource/python/test.txt') if isdir(r'/data/webroot/resource/python') == True else True
总是 False 受不了
2018-05-31
感觉这题的关键就是值在堆和栈中的变化,f1(),f2(),f3()时,其中的i值是否还存在. 如果 i 的值不会在创建count()执行结束后出栈, 那么循环直接写在外面就好了
2018-05-30
def count():
fs = []
for i in range(1, 4):
def f(x=i): #这里不能引用外部循环变量,进行一次替换
return x*x
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(x=i): #这里不能引用外部循环变量,进行一次替换
return x*x
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-05-27
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
#str.lower将字符串中所有大写字母转换成小写并返回
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
#str.lower将字符串中所有大写字母转换成小写并返回
2018-05-27
class Person(object):
def __init__(self, name, score):
self.name = name
if score >=90 :
self.__score = 'A-优秀'
elif score >= 60 :
self.__score = 'B-及格'
else:
self.__score = 'C-不及格'
def get_grade(self):
return self.__score
def __init__(self, name, score):
self.name = name
if score >=90 :
self.__score = 'A-优秀'
elif score >= 60 :
self.__score = 'B-及格'
else:
self.__score = 'C-不及格'
def get_grade(self):
return self.__score
2018-05-26
class Person(object):
__count = 0
def __init__(self, name):
self.name=name
Person.__count += 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
print 'AttributeError'
__count = 0
def __init__(self, name):
self.name=name
Person.__count += 1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
print 'AttributeError'
2018-05-26
def format_name(s):
# return s[0].upper()+s[1:].lower()
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
# return s[0].upper()+s[1:].lower()
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-05-26
def __cmp__(self, s):
if self.score != s.score:
return -cmp(self.score, s.score)
else:
return cmp(self.name, s.name)
这样写也是可以的
if self.score != s.score:
return -cmp(self.score, s.score)
else:
return cmp(self.name, s.name)
这样写也是可以的
2018-05-25
你们这些直接复制标准答案的,就别发到评论区来丢人了,唉。。
说真的,这节真够BT的,我也是看了参考答案,还是一脸懵逼。
你确定time.time这个有讲过?
还有%s %fs这些???
说真的,这节真够BT的,我也是看了参考答案,还是一脸懵逼。
你确定time.time这个有讲过?
还有%s %fs这些???
2018-05-23