提高水平總是靠學習他人代碼來的快
看看這個代碼,摘錄自gameobject
1 def __init__(self, x=0., y=0.):
2 """Initialise a vector
3
4 @type x: number
5 @param x: The x value (defaults to 0.), or a container of 2 values
6 @type x: number
7 @param y: The y value (defaults to 0.)
8
9 """
10 if hasattr(x, "__getitem__"):
11 x, y = x
12 self._v = [float(x), float(y)]
13 else:
14 self._v = [float(x), float(y)]
挺好的,參數帶入可以支持 a(1,2)形式,也可支持a( (1,2) )形式 ,非常靈活,自已以前總是想不到用hasattr來判斷