p1 = Person('Bob', 90)
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
print p1.get_grade()
print p2.get_grade()
print p3.get_grade()
p2 = Person('Alice', 65)
p3 = Person('Tim', 48)
print p1.get_grade()
print p2.get_grade()
print p3.get_grade()
2015-03-25
def get_grade(self):
if self.__score > 80:
return "A-优秀"
elif self.__score >= 60:
return "B-及格"
else:
return "C-不及格"
if self.__score > 80:
return "A-优秀"
elif self.__score >= 60:
return "B-及格"
else:
return "C-不及格"
2015-03-25
#encoding=utf8
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
class Person(object):
def __init__(self, name, score):
self.name = name
self.__score = score
2015-03-25
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count+1
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count+1
p1 = Person('Bob')
p2 = Person('Alice')
print Person.__count
2015-03-25
def format_name(s):
return s[0].upper()+s[1:]
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:]
print map(format_name, ['adam', 'LISA', 'barT'])
2015-03-24
print filter(lambda x:True if x and x.strip() else False, ['test', None, '', 'str', ' ', 'END'])
2015-03-23
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2015-03-23
class Rational(object):
def __init__(self, p, q):
self.p = p
self.q = q
def __int__(self):
return self.p // self.q
def __float__(self):
return (self.p / float(self.q))
print float(Rational(7, 2))
print float(Rational(1, 3))
def __init__(self, p, q):
self.p = p
self.q = q
def __int__(self):
return self.p // self.q
def __float__(self):
return (self.p / float(self.q))
print float(Rational(7, 2))
print float(Rational(1, 3))
2015-03-23
import time
def performance(f):
def fn(*args, **kw):
t1 = time.time()
t2 = time.time()
print 'call %s() in %fs' % (f.__name__, (t2 - t1))
return f(*args, **kw)
return fn
def performance(f):
def fn(*args, **kw):
t1 = time.time()
t2 = time.time()
print 'call %s() in %fs' % (f.__name__, (t2 - t1))
return f(*args, **kw)
return fn
2015-03-23