Python的Method理解

Python的Method理解

定義

在介紹Python的Method之前需要先來認識一下Python中的function


function-函數

  • 函數在Python中的理解是可以通過函數名(function_name)進行調用的,並且函數可能會帶有參數,也就是放在圓括號中的值function_name(args), 函數執行某種行爲並且返回一個值

注意: 這裏並不是所有的Python函數都存在返回值。


如何檢測Python中的function-函數可以調用了?

Python2版本中的內置函數callable()進行檢測

C:\Users\JackDan9>python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> a = 1
>>> callable(a)
False
>>> b = sys.path
>>> callable(b)
False
>>> c = sys.argv
>>> callable(c)
False
>>> d = sys.getdefaultencoding
>>> callable(d)
True
>>> type(c)
<type 'list'>
>>> type(d)
<type 'builtin_function_or_method'>
>>> type(b)
<type 'list'>
>>> type(a)
<type 'int'>
>>>
  • 結合上面的示例我們可以知道,那些事可以提供給我們進行調用的function-函數了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章