class vector2d:
...: typecode = 'd'
...: def __init__(self,x,y):
...: self.__x = float(x) # 私有變數
...: self.__y = float(y)
...: @property
...: def x(self):
...: # 讀取,v1.x相同方式讀取
...: return self.__x
...: @property
...: def y(self):
...: return self.__y
...: def __iter__(self):
...: return (i for i in (self.x,self.y))
...: def __repr__(self):
# eval(repr(v1))==v1
...: class_name = type(self).__name__
...: return '{}(,)'.format(class_name,*self)
...: def __str__(self):
# print呼叫該方法
...: return str(tuple(self))
...: def __bytes__(self):
# 轉為位元組碼
...: return (bytes([ord(self.typecode)]) + bytes(array(self.typecode,self)))
...: @classmethod
...: def from_bytes(cls,octets):
# vector2d.from_bytes(bytes(v1)) == v1
...: typecode = chr(octets[0])
...: memv = memoryview(octets[1:]).cast(typecode) # memoryview是陣列的一種
...: return cls(*memv)
...: def __eq__(self,other):
# 注意特性[3.0,4.0] == print(v1)
...: return tuple(self) == tuple(other)
...: def __abs__(self):
# 計算模長
...: return math.hypot(self.x,self.y)
def __bool__(self):
# bool(v1),若abs(v1)==0則返回flase
...: return bool(abs(self))
...: def angle(self):
...: return math.atan2(self.y,self.x)
...: def __format__(self,fmt_spec=''):
# 極座標形式
...: if fmt_spec.endswith('p'):
...: fmt_spec = fmt_spec[:-1]
...: cood = (abs(self),self.angle())
...: outer_fmt = '<{},{}>'
...: else:
...: cood = self
...: outer_fmt = '({},{})'
...: components = (format(c,fmt_spec) for c in cood) # 數字的小格式
...: return outer_fmt.format(*components)
...: def __hash__(self):
# 雜湊化
...: return hash(self.x)^hash(self.y)
class vector:
...: typecode = 'd'
...: def __init__(self,components):
...: self._components = array(self.typecode,components)
...: def __iter__(self):
...: return iter(self._components)
...: def __repr__(self):
...: components = reprlib.repr(self._components) # 將超長變數變成...
...: components = components[components.find('['):-1] # 將陣列形式進行提取,find函式返回位置
...: return 'vector({})'.format(components)
...: def __str__(self):
...: return str(tuple(self))
...: def __bytes__(self):
...: return (bytes([ord(self.typecode)])+bytes(self._components))
...: def __hash__(self):
...: hashes = map(hash,self._components)
...: return functools.reduce(operator.xor,hashes,0)# 異或運算需要設定0為初始值,以防報錯
...: def __eq__(self,other):
...: return (len(self)==len(other)) and all(a==b for a,b in zip(self,other))
...: def __abs__(self):
...: return math.sqrt(sum(x*x for x in self))
...: def __bool__(self):
...: return bool(abs(self))
...: def __len__(self):
...: return len(self._components)
...: def __getitem__(self,index):
...: # 定義了該方法和len方法就是實現鴨子型別,序列
...: cls = type(self)# 備用
...: if isinstance(index,slice):
...: return cls(self._components[index]) # 如果是切片則返回例項物件
...: def __getattr__(self,name):
...: # 當屬性查詢失敗的時候會呼叫該方法
...: cls = type(self)
...: if len(name)==1:
...: pos = cls.shortcut_names.find(name)
...: if 0<=pos'
...: else:
...: coords = self
...: out_fmt = '({})'
...: components = (format(c,fmt_spec) for c in coords)
...: return out_fmt.format(','.join(components))
...: @classmethod
...: def frombytes(cls,octets): # 類方法的第乙個引數必定是cls
...: typecode = chr(octets[0])
...: memv = memoryview(octets[1:]).cast(typecode)
...: return cls(memv) # 返回乙個類物件,不再拆包
Python常見的特殊方法
一 重寫 repr 方法 類的自我描述方法 當列印物件時,系統會根據 repr 方法輸出物件的自我描述資訊,用來告訴外界該物件具有的狀態資訊 class rectangle def init self,width,height self.width width self.height height ...
Python中的常見特殊方法 del方法
del 方法用於銷毀python物件 在任何python物件將被系統 的時候,系統都會自動呼叫這個方法。但是不要以為對乙個變數執行del操作,該變數引用的物件就會被 當然不是,如果有多個變數引用它,del其中乙個變數是不會 該物件的。栗子如下 1 coding utf 8 2class item 3...
Python中的常見特殊方法 repr方法
在python中有些方法名 屬性名的前後都新增了雙下劃線,這種方法 屬性通常都屬於python的特殊方法和特殊屬性,開發者可以通過重寫這些方法或者直接呼叫這些方法來實現特殊的功能。其實前面見過的構造方法 init 就是一種特殊方法,開發者可以通過重寫類中的 init 方法實現自己想要的初始化邏輯。p...