#建立類 class Student1: pass #建立類,繼承自object類 class Student1(object): pass #帶構造方法的類以及對象方法調用 class Student2(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print('%s: %s' % (self.name, self.score))
##實例化和調用python
#實例化類 >>> bart = Student1() >>> bart <__main__.Student object at 0x10a67a590> >>> Student1 <class '__main__.Student'> >>> bart = Student2('Bart Simpson', 59) >>> bart.print_score() Bart Simpson: 59 #綁定屬性 >>> bart.name = 'Bart Simpson' >>> bart.name 'Bart Simpson'
__
: 私有變量(解釋器把雙下劃線改爲了單下劃線,依舊能夠經過單下劃線訪問)_
: 能夠被外部訪問,但約定俗稱不要訪問__XXX__
: 特殊變量,能夠訪問#父類、基類或者超類 class Animal(object): def run(self): print('Animal is running...') #子類 class Dog(Animal): pass class Cat(Animal): pass
在繼承關係中,若是一個實例的數據類型是某個子類,那它的數據類型也能夠被看作是父類。可是,反過來就不行。函數
type()
: 判斷對象類型isinstance()
:對象是不是某種類型dir()
:對象的全部屬性和方法hasattr(obj, 'x')
:有屬性'x'嗎?setattr(obj, 'y', 19)
:設置一個屬性'y'getattr(obj, 'y')
:獲取屬性'y',屬性不存在,拋出AttributeError
class Student(object): #類屬性 name = 'Student' #實例屬性 def __init__(self, name): self.name = name
#給對象綁定方法 >>> def set_age(self, age): # 定義一個函數做爲實例方法 ... self.age = age ... >>> from types import MethodType >>> s.set_age = MethodType(set_age, s) # 給實例綁定一個方法 >>> s.set_age(25) # 調用實例方法 >>> s.age # 測試結果 25 #給類綁定方法 >>> def set_score(self, score): ... self.score = score ... >>> Student.set_score = MethodType(set_score, Student)
__slots__
限制只能添加指定屬性class Student(object): __slots__ = ('name', 'age') # 用tuple定義容許綁定的屬性名稱 >>> s = Student() # 建立新的實例 >>> s.name = 'Michael' # 綁定屬性'name' >>> s.age = 25 # 綁定屬性'age' >>> s.score = 99 # 綁定屬性'score' Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Student' object has no attribute 'score'
@property
簡化setter和getterclass Student(object): @property def score(self): return self._score @score.setter def score(self, value): if not isinstance(value, int): raise ValueError('score must be an integer!') if value < 0 or value > 100: raise ValueError('score must between 0 ~ 100!') self._score = value #當調用屬性賦值時,自動調用對應的setter方法 >>> s = Student() >>> s.score = 60 # OK,實際轉化爲s.set_score(60) >>> s.score # OK,實際轉化爲s.get_score() 60 >>> s.score = 9999 Traceback (most recent call last): ... ValueError: score must between 0 ~ 100!
class Dog(Mammal, Runnable): pass #須要「混入」額外的功能,使用叫作 MixIn 的設計 class Dog(Mammal, RunnableMixIn, CarnivorousMixIn): pass
__str__
:相似於Java中的toString()方法__repr__()
:調試時使用的字符串__iter__
:返回迭代對象,用於for ... in
循環__getitem__
:使用下標方式獲取元素__getattr__
:沒有找到屬性的狀況下調用此方法__call__
:把對象當作函數而直接調用所執行的方法from enum import Enum Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) #遍歷 for name, member in Month.__members__.items(): print(name, '=>', member, ',', member.value) #派生 from enum import Enum, unique @unique class Weekday(Enum): Sun = 0 # Sun的value被設定爲0 Mon = 1 Tue = 2 Wed = 3 Thu = 4 Fri = 5 Sat = 6 #@unique用於檢查重複
type()
>>> from hello import Hello >>> h = Hello() >>> h.hello() Hello, world. >>> print(type(Hello)) <class 'type'> >>> print(type(h)) <class 'hello.Hello'>
#1.class名,2.父類集合,3.class的方法名稱與函數綁定 >>> def fn(self, name='world'): # 先定義函數 ... print('Hello, %s.' % name) ... >>> Hello = type('Hello', (object,), dict(hello=fn)) # 建立Hello class >>> h = Hello() >>> h.hello() Hello, world. >>> print(type(Hello)) <class 'type'> >>> print(type(h)) <class '__main__.Hello'>