2017-04-10 47 views
1

我是Python新手,我的意思是它們如何實現的區別。Python中對象的屬性和方法之間的聯繫是什麼?

例如:

>>> a=np.array([1,2,5,3,43]) 
>>> a.sort() 
>>> a 
array([ 1, 2, 3, 5, 43]) 
>>> a=np.array([1,2,5,3,43]) 
>>> a.shape 
(5,) 
>>> a.sort() 
>>> a 
array([ 1, 2, 3, 5, 43]) 
>>> a.sort 
<built-in method sort of numpy.ndarray object at 0x7f78e358a9e0> 
>>> a.shape() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'tuple' object is not callable 

也許答案應該是像在Python

+0

屬性是一個值。一種方法是一個「功能」。 – DyZ

+0

@DYZ我的意思是他們之間有什麼**鏈接**?我聽說過_ _ foo _ _,關於「__」的任何事情? – Statham

+0

沒有鏈接。他們是無關的,除了兩者屬於同一個對象。 – DyZ

回答

1

一種方法,特殊用途的「_ _ _ foo_」是與對象相關聯的功能。當你輸入一個.sort時,它返回存儲該函數的內存中的地址,如果使用括號,它將調用該函數。屬性只是對象中的一個變量,所以當你調用一個.shape()函數時,它會給你一個錯誤,因爲你試圖調用一個變量作爲函數。

不知道這是你要找的人,但我希望它幫

相關問題