def cmp_ignore_case(s1, s2):
if s1[0].lower() > s2[0].lower():
return 1
if s1[0].lower() < s2[0].lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].lower() > s2[0].lower():
return 1
if s1[0].lower() < s2[0].lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-03
可以使用类名调用类属性和类方法,但不能用类名调用实例属性和实例方法;
可以是用实例化对象调用实例属性和实例方法,也可以调用类属性和类方法
可以是用实例化对象调用实例属性和实例方法,也可以调用类属性和类方法
2018-01-03
@property
def grade(self):
if self.__score >= 80:
return 'A'
elif self.__score < 60:
return 'C'
else:
return 'B'
def grade(self):
if self.__score >= 80:
return 'A'
elif self.__score < 60:
return 'C'
else:
return 'B'
2018-01-03
a,b,L = 0,1,[]
for x in range(num):
L.append(a)
a,b = b,a + b
self.numbers = L
for x in range(num):
L.append(a)
a,b = b,a + b
self.numbers = L
2018-01-03
if self.score == s.score:
return cmp(self.name, s.name)
return -cmp(self.score, s.score)
return cmp(self.name, s.name)
return -cmp(self.score, s.score)
2018-01-03
__score = 65
def __init__(self, name, score):
self.name = name
self.score = score
def get_grade(self):
if self.score == Person.__score :
return 'B-及格'
elif self.score > Person.__score :
return 'A-优秀'
def __init__(self, name, score):
self.name = name
self.score = score
def get_grade(self):
if self.score == Person.__score :
return 'B-及格'
elif self.score > Person.__score :
return 'A-优秀'
2018-01-03
class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k, v in kw.items():
setattr(self, k, v)
p = Person('Bob', 'Male', age=18, course='Python')
print(p.age)
print(p.course)
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k, v in kw.items():
setattr(self, k, v)
p = Person('Bob', 'Male', age=18, course='Python')
print(p.age)
print(p.course)
2018-01-02