最新回答 / 慕后端4177492
#coding=utf-8? ?class Animal(): passdog = Animal()dog.name = "旺財"dog.age = 2cat = Animal()cat.name = 'Tom'cat.age = 3
2022-04-29
最新回答 / 土斤土斤
__slots__ 只能限制為實例對象動態添加屬性和方法,而無法限制動態地為類添加屬性和方法。因為你是直接往類里面添加屬性和方法,所以實例s能訪問到age這個Student類的屬性
2022-04-26
最新回答 / Dreamweaver3662984
class Person:? ? def __init__(self, name, gender):? ? ? ? self.name = name? ? ? ? self.gender = genderclass SkillMixin:? ? def __init__(self, skill):? ? ? ? self.skill = skill? ? def say(self):? ? ? ? print("技能是 %s" %(self.skill))class BasketballMixin(Ski...
2022-04-24
最新回答 / 打字的狐貍
實例的屬性定義函數中也不能直接調用私有的類屬性,需要加一個類方法,在實例的屬性定義中: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