-
class Animal():
??? def __init__(self,age,name,location):
??????? self.__age = age
??????? self.__name = name
??????? self.__location = location
??? def get_name(self):#通過定義實例方法來操作私有屬性
??????? return self.__name
??? def get_age(self):#通過定義實例方法來操作私有屬性
??????? return self.__age
??? def get_location(self):#通過定義實例方法來操作私有屬性
??????? return self.__location
dog = Animal(1,"sisi","Shanghai")
bird = Animal(12, "gigi", "Anhui")
cat = Animal(3, "mimi", "Canada")
print(dog.get_age())
print(bird.get_name())
print(cat.get_location())查看全部 -
在外部訪問私有屬性將會拋出異常,提示沒有這個屬性。
雖然私有屬性無法從外部訪問,但是,從類的內(nèi)部是可以訪問的。私有屬性是為了保護類或?qū)嵗龑傩圆槐煌獠课廴径O(shè)計的。# 類私有屬性
class Animal(object):
? ?__localtion = 'Asia'
print(Animal.__localtion)
Traceback (most recent call last):
?File "<stdin>", line 1, in <module>
AttributeError: type object 'Animal' has no attribute '__localtion'# 實例私有屬性
class Animal(object):
? ?def __init__(self, name, age, localtion):
? ? ? ?self.name = name
? ? ? ?self.age = age
? ? ? ?self.__localtion = localtion
dog = Animal('wangwang', 1, 'GuangDong')
print(dog.name) # ==> wangwang
print(dog.age) # ==> 1
print(dog.__localtion)
Traceback (most recent call last):
?File "<stdin>", line 1, in <module>
AttributeError: 'Animal' object has no attribute '__localtion'查看全部 -
'''請定義一個動物類
抽象出名字、年齡兩個屬性
并實例化兩個實例dog, cat。
'''
class Animal:
??? def __init__(self,name,age):
??????? self.name = name
??????? self.age = age
dog = Animal("wangwang",2)
cat = Animal("cici",3)
print(dog.name)
print(cat.age)查看全部 -
'''請定義一個動物類
并創(chuàng)建出兩個實例dog,?cat
分別賦予不同的名字和年齡
并打印出來'''
class Animal:
??? def __init__(self,name,age):
??????? self.name = name
??????? self.age = age
dog = Animal("chichi", 2)
cat = Animal("mike", 6)
print(dog)
print(cat)查看全部 -
'''請練習定義一個動物類
并創(chuàng)建出兩個實例dog,?cat
打印實例
再比較兩個實例是否相等'''
class Animal:pass
dog = Animal()
cat = Animal()
print(dog)
print(cat)
print(dog == cat)查看全部 -
mark
查看全部 -
試試有沒有人在看試試有沒有人在看查看全部
-
class Student(Person):
? ?def __init__(self, name, gender, score):
? ? ? ?super(Student, self).__init__(name, gender)查看全部 -
實例屬性的優(yōu)先級高于類屬性
外部無妨訪問私有屬性(__),可以通過定義實例屬性來訪問
查看全部 -
reduce(函數(shù),list,初始化值)
查看全部 -
不是純函數(shù)式編程:允許有變量
支持高階函數(shù):函數(shù)可以作為變量
支持閉包:可以返回函數(shù)
支持匿名函數(shù)。
查看全部 -
functools.partial(原函數(shù),改變默認參數(shù))
查看全部 -
有必要注意的是,返回函數(shù)和返回函數(shù)值的語句是非常類似的,返回函數(shù)時,不能帶小括號,而返回函數(shù)值時,則需要帶上小括號以調(diào)用函數(shù)。
查看全部 -
解題思路:
如果一個數(shù)的平方根轉(zhuǎn)化為整型,然后再平方等于原來的數(shù),則它的平方根為整數(shù)
查看全部 -
注意: s.strip()會默認刪除空白字符(包括'\n', '\r', '\t', ' '),如下:
s = ' ? ? 123'
s.strip() # ==> 123
s= '\t\t123\r\n'
s.strip() # ==> 123查看全部
舉報