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'
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'
2016-05-06
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count+1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2016-05-06
sorted_ignore_case = functools.partial(sorted,key=str.upper)
#或者参数key = str.lower都可以
#或者参数key = str.lower都可以
2016-05-06
已采纳回答 / xdp
Python中的reduce(func,list,initial),其中initial表示累计初始值,是可选参数,如果省略,则系统默认为list[0],我记得这个一节课程中的f函数是计算两个数乘积的,所以initial=1表示list中的所有数从乘1开始,如果是2的话就从乘2开始,你可以自己改变下参数试试
2016-05-06
def __init__(self, name, score):
self__name=name
self__score=score
if self__score>80:
print 'A-优秀'
elif self__score>60:
print 'B-及格'
else:
print 'C-不及格'
self__name=name
self__score=score
if self__score>80:
print 'A-优秀'
elif self__score>60:
print 'B-及格'
else:
print 'C-不及格'
2016-05-06
class Person(object):
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print AttributeError
def __init__(self, name, score):
self.name=name
self.__score=score
p = Person('Bob', 59)
print p.name
try:
print p.__score
except:
print AttributeError
2016-05-06
这两个是python中的可变参数。*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前
2016-05-06
import time
def performance(unit):
def decorator(f):
def fn(*args,**kw):
print 'call %s() in %s'%(f.__name__,unit)
return f(*args,**kw)
return fn
return decorator
@performance('ms')
.......
def performance(unit):
def decorator(f):
def fn(*args,**kw):
print 'call %s() in %s'%(f.__name__,unit)
return f(*args,**kw)
return fn
return decorator
@performance('ms')
.......
2016-05-06
import json
class Students(object):
def __init__(self):
self.names= r'["Tim", "Bob", "Alice"]'
def read(self):
return self.names
s = Students()
print json.load(s)
class Students(object):
def __init__(self):
self.names= r'["Tim", "Bob", "Alice"]'
def read(self):
return self.names
s = Students()
print json.load(s)
2016-05-05
import time
def performance(unit):
def decorator(f):
def fn(*args,**kw):
print 'call %s() in %s'%(f.__name__,unit)
return f(*args,**kw)
return fn
return decorator
def performance(unit):
def decorator(f):
def fn(*args,**kw):
print 'call %s() in %s'%(f.__name__,unit)
return f(*args,**kw)
return fn
return decorator
2016-05-05
def performance(f):
def fn(*args, **kw):
start = time.time() * 1000
result = f(*args, **kw);
end = time.time() * 1000
print 'call ' + f.__name__ + '() in ' + str(end - start) + 'ms'
return result
return fn;
def fn(*args, **kw):
start = time.time() * 1000
result = f(*args, **kw);
end = time.time() * 1000
print 'call ' + f.__name__ + '() in ' + str(end - start) + 'ms'
return result
return fn;
2016-05-05
def calc_prod(lst):
def calc_delay():
s=1
for x in lst:
s=s*x
return s
return calc_delay
f = calc_prod([1, 2, 3, 4])
print f()
def calc_delay():
s=1
for x in lst:
s=s*x
return s
return calc_delay
f = calc_prod([1, 2, 3, 4])
print f()
2016-05-04