最新回答 / 打字的狐狸
实例的属性定义函数中也不能直接调用私有的类属性,需要加一个类方法,在实例的属性定义中:class Animal(object): __count=0 def __init__(self,name,age): self.name=name self.age=age Animal.set_count() @classmethod def set_count(cls): cls.__count+=1 @classmethod ...
2022-04-23
class Person(object):
def __init__(self, name,age,location):
self.__name = name
self.__age=age
self.__location=location
def get(self):
return '{},{},{}'.format(self.__name,self.__age,self.__location)
p = Person('Alice',22,'hongkong')
print(p.get())
def __init__(self, name,age,location):
self.__name = name
self.__age=age
self.__location=location
def get(self):
return '{},{},{}'.format(self.__name,self.__age,self.__location)
p = Person('Alice',22,'hongkong')
print(p.get())
2022-04-23
更习惯这种写法:
def is_odd(x):
return x % 2 == 1
item =filter(is_odd, [1, 4, 6, 7, 9, 12, 17])
print(item)
def is_odd(x):
return x % 2 == 1
item =filter(is_odd, [1, 4, 6, 7, 9, 12, 17])
print(item)
2022-04-17
def format_name(s):
return s.title()
item = map(format_name, ['alice', 'BOB', 'CanDY'])
print(item)
这样也行,更容易理解。很奇怪,答案中为什么没有报错,还是不理解python运行的内部机制
return s.title()
item = map(format_name, ['alice', 'BOB', 'CanDY'])
print(item)
这样也行,更容易理解。很奇怪,答案中为什么没有报错,还是不理解python运行的内部机制
2022-04-17
它这也是要实例化之后才能使用的,跟直接定义一个方法,然后访问p.calls()有何不同?实际使用场景是什么呢?
2022-03-18
最新回答 / weixin_慕圣3493772
你在BasStudent里面两次调用了super方法,一厢情愿地认为会分别调用两个父类的init方法,但实际上不是,所以程序报错认为缺参数(可能是两次调用了同一个三参数的父类init方法)。具体原因我也没搞清楚,不过网上的忠告:不惜一切代价地避免多重继承,它带来的麻烦比能解决的问题都多。如果你非要用,那你得准备好专研类的层次结构,以及花时间去找各种东西的来龙去脉吧!
2022-03-05
最新回答 / 慕前端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