最新回答 / 慕前端7080484
class BasStudent(Student,BasketballMixin): def __init__(self,name,gender,score,skill,basketball): super(BasStudent, self).__init__(name,gender,score) def getskill(n,k): print("我叫 %s,我会打%s "%(n,k))a=Student('jiji','boy',13)b=BasketballM...
2022-03-04
已采纳回答 / 慕前端7080484
直接输入变量,调用的是__repr__()方法,而__repr__()用于显示给开发人员。而当使用str()时,实际调用的是__str__()方法,所以要用str()来转换。下载视频
2022-03-04
最新回答 / 高飞的鱼
f = open("./test.txt",'r'),这里可以换成具体的文件路径,比如,
f = open('/Users/lihui/Desktop/hoho.txt', 'r')
2022-03-03
后面的例子可以理解成:
调用count()时给fs这个list每个位置都赋值了一个 i*i的函数,但因为返回值是函数f本身所以并没有调用,即fs = [i*i, i*i, i*i],此时里面的i并不表示实际值;当count()被调用完后for循环里的i已经到3,所以当调用fs这个list内的函数f1/f2/f3时都计算i*i,此时i代入3,所以三个结果都为9。
调用count()时给fs这个list每个位置都赋值了一个 i*i的函数,但因为返回值是函数f本身所以并没有调用,即fs = [i*i, i*i, i*i],此时里面的i并不表示实际值;当count()被调用完后for循环里的i已经到3,所以当调用fs这个list内的函数f1/f2/f3时都计算i*i,此时i代入3,所以三个结果都为9。
2022-02-15
class Animal:
__count=0
def __init__(self,name):
self.name=name
Animal.__count+=1
@classmethod
def get_count(self):
return self.__count
dog=Animal('liu')
print(dog.get_count())
cat=Animal('wang')
print(cat.get_count())
__count=0
def __init__(self,name):
self.name=name
Animal.__count+=1
@classmethod
def get_count(self):
return self.__count
dog=Animal('liu')
print(dog.get_count())
cat=Animal('wang')
print(cat.get_count())
2022-02-11
最赞回答 / weixin_慕用0068683
如果count改成__count变成类的私有属性后,在__init__方法里无法直接__count访问类的私有属性,需要Animal.__count,尝试了下self.__count也可以,我理解是__count本身是类的属性,任何一个实例并不单独具有这个属性,但是可以通过实例调用类的get和set方法去修改类的属性,就和直接用类名调用get和set方法效果是一样的,比如下面的代码里animal.set和dog.set都可以修改类的属性__count。<...code...>
2022-02-11