最新回答 / 打字的狐貍
實例的屬性定義函數中也不能直接調用私有的類屬性,需要加一個類方法,在實例的屬性定義中: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