def __str__(self):
return '(Student: %s, %s, %s)' % (self.name, self.gender, self.score)
return '(Student: %s, %s, %s)' % (self.name, self.gender, self.score)
2018-03-26
class Person(object):
def __init__(self, name):
self.name = name
xiaoming = Person("xiaoming")
xiaohong = Person(name="xiaohong")
print xiaoming.name
print xiaohong.name
print("hahaha!")
def __init__(self, name):
self.name = name
xiaoming = Person("xiaoming")
xiaohong = Person(name="xiaohong")
print xiaoming.name
print xiaohong.name
print("hahaha!")
2018-03-26
class BStudent(Student, BasketballMixin):
def __init__(self):
super(Student, self).__init__()
super(BasketballMixin, self).__init__()
def __init__(self):
super(Student, self).__init__()
super(BasketballMixin, self).__init__()
2018-03-26
import math
def is_sqr(x):
return int(math.sqrt(x))**2==x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return int(math.sqrt(x))**2==x
print filter(is_sqr, range(1, 101))
2018-03-26
class Students(object):
def __init__(self):
pass
def read(self):
return r'["Tim", "Bob", "Alice"]'
def __init__(self):
pass
def read(self):
return r'["Tim", "Bob", "Alice"]'
2018-03-26
python 2和 python 3 通用:
L2 = sorted(L1,key = lambda x:x.name)
老师里面的答案只适用2.7及以下
L2 = sorted(L1,key = lambda x:x.name)
老师里面的答案只适用2.7及以下
2018-03-26
class Person(object):
__count = 0
def __init__(self,name):
self.name = name
Person.__count = Person.__count + 1
@classmethod
def how_many(cls):
return cls.__count
__count = 0
def __init__(self,name):
self.name = name
Person.__count = Person.__count + 1
@classmethod
def how_many(cls):
return cls.__count
2018-03-26
class Person(object):
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-不及格')
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-不及格')
2018-03-26
import math
def is_sqr(x):
return str(math.sqrt(x)).split('.')[1]=='0'
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return str(math.sqrt(x)).split('.')[1]=='0'
print filter(is_sqr, range(1, 101))
2018-03-26
class Person(object):
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
setattr(self, kw.items()[0][0], kw.items()[0][1])
参考:http://www.runoob.com/python/python-func-setattr.html
def __init__(self, name, gender, birth, **kw):
self.name = name
self.gender = gender
self.birth = birth
setattr(self, kw.items()[0][0], kw.items()[0][1])
参考:http://www.runoob.com/python/python-func-setattr.html
2018-03-26
最新回答 / 慕斯6415185
f.__name__(双下划线)就是函数f的函数名,你可以在ide里面试一下,f=abs,f.__name__,会返回abs。你的代码fn函数没有返回值,要return fn,即返回函数名
2018-03-26