class FTeacher(Teacher, FootballMixin):
def __init__(self, a, skill):
super(FTeacher, self).__init__(a)
super(FootballMixin, self).__init__(skill)
b = BStudent('Tom', 'basketball')
print b.skill
k = FTeacher('Peter', 'football')
print k.skill
def __init__(self, a, skill):
super(FTeacher, self).__init__(a)
super(FootballMixin, self).__init__(skill)
b = BStudent('Tom', 'basketball')
print b.skill
k = FTeacher('Peter', 'football')
print k.skill
2017-10-12
class BStudent(Student, BasketballMixin):
def __init__(self, a, skill):
super(BStudent, self).__init__(a)
super(BasketballMixin, self).__init__(skill)
def __init__(self, a, skill):
super(BStudent, self).__init__(a)
super(BasketballMixin, self).__init__(skill)
2017-10-12
class SkillMixin(object):
def __init__(self, skill):
self.skill = skill
class BasketballMixin(SkillMixin):
def skill(self):
return 'basketball'
class FootballMixin(SkillMixin):
def skill(self):
return 'football'
def __init__(self, skill):
self.skill = skill
class BasketballMixin(SkillMixin):
def skill(self):
return 'basketball'
class FootballMixin(SkillMixin):
def skill(self):
return 'football'
2017-10-12
看了全部评论,没有一个正确答案,只能自己写了:
class Person(object):
def __init__(self, a):
self.a = a
class Student(Person):
def __init__(self, a):
super(Student, self).__init__(a)
class Teacher(Person):
def __init__(self, a):
super(Teacher, self).__init__(a)
class Person(object):
def __init__(self, a):
self.a = a
class Student(Person):
def __init__(self, a):
super(Student, self).__init__(a)
class Teacher(Person):
def __init__(self, a):
super(Teacher, self).__init__(a)
2017-10-12
class Student(Person):
__slots__ =('score')
#slots可以继承自父类,子类想添加属性只需把新的属性用slots定义一遍即可
def __init__(self,name,gender,score):
Person.__init__(self,name,gender)
#继承可以不用super.格式:父类.__init__(self,想继承的属性)
self.score = score
__slots__ =('score')
#slots可以继承自父类,子类想添加属性只需把新的属性用slots定义一遍即可
def __init__(self,name,gender,score):
Person.__init__(self,name,gender)
#继承可以不用super.格式:父类.__init__(self,想继承的属性)
self.score = score
2017-10-11
1.@property,可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式
2.用 @property 修饰的 "get "作用的方法拥有只读属性,如果想实现可写属性,则必须配套使用@方法名.setter来编写一个set函数
2.用 @property 修饰的 "get "作用的方法拥有只读属性,如果想实现可写属性,则必须配套使用@方法名.setter来编写一个set函数
2017-10-11
python 会将 __xxx (双下划线) 属性 转换为 _classname__xxx (在此属性名前 添加 单下划线和类名),所以访问__xxx 不行,需要访问 _classname_xxx 。通过 classname.__dict__ 查看 类 的属性
2017-10-10
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(AttributeError):
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(AttributeError):
print 'attributeerror'
2017-10-10
raise ValueError('invalid score')手工引发一个异常,raise后接异常名和初始化参数
2017-10-10
最大公约数算法
方法一:
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
方法二:
def gcd(a, b):
if a < b:
a, b = b, a
while b != 0:
temp = a % b
a = b
b = temp
return a
方法一:
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
方法二:
def gcd(a, b):
if a < b:
a, b = b, a
while b != 0:
temp = a % b
a = b
b = temp
return a
2017-10-10
def __str__(self):
return str(self.numbers)
#__str__函数只能返回str类型的量
return str(self.numbers)
#__str__函数只能返回str类型的量
2017-10-10
class Students(object):
def __init__(self, name): #括号里的是参数名而不是属性名
self.shuxing = name #不要混淆
def __init__(self, name): #括号里的是参数名而不是属性名
self.shuxing = name #不要混淆
2017-10-10
def __cmp__(self, s):
if self.score == s.score:
return cmp(self.name, s.name)
return -cmp(self.score, s.score)
#在我们自己定义的Student类中,sorted函数只是不知道该cmp什么,只需要调用cmp函数告诉他参数试self.name即可,不用亲自再把等于1,-1,0的cmp函数重新定义一遍
if self.score == s.score:
return cmp(self.name, s.name)
return -cmp(self.score, s.score)
#在我们自己定义的Student类中,sorted函数只是不知道该cmp什么,只需要调用cmp函数告诉他参数试self.name即可,不用亲自再把等于1,-1,0的cmp函数重新定义一遍
2017-10-10