close
引述Greeksforgreeks—Difference between Method and Function in Python 大概是這麼說
1. Simply, function and method both look similar as they perform in almost similar way, but the key difference is the concept of ‘Class and its Object‘.
2. Functions can be called only by its name, as it is defined independently. But methods can’t be called by its name only, we need to invoke the class by a reference of that class in which it is defined, i.e. method is defined within a class and hence they are dependent on that class.
我理解的大概是
(學屬理論上說:)…(待補充)
(技術使用的說:)
1. 觀念上,兩者扮演的功能角色是相同,不同的是method是源自於class和他產生出來的物件
2. function呼叫時直接用其名,但method不可只用method的名,必須涵蓋它源自於得類(class)…(待補充)
(直白一點的說:)
來自於class的稱為method,沒有class的稱為function
詳細補充的內容如下(含說明實例)
Python Method
- 1.Method is called by its name, but it is associated to an object (dependent).
- 2.A method is implicitly passed the object on which it is invoked.
- 3.It may or may not return any data.
- 4.A method can operate on the data (instance variables) that is contained by the corresponding class
Basic Method Structure in Python :
output
Python 3 Inbuilt method :
output
Python Functions
- 1.Function is block of code that is also called by its name. (independent)
- 2.The function can have different parameters or may not have any at all. If any data (parameters) are passed, they are passed explicitly.
- 3.It may or may not return any data.
- 4.Function does not deal with Class and its instance concept.
Python 3 User-Defined Function :
Output
Python 3 Inbuilt Function :
參考文件:
https://www.geeksforgeeks.org/difference-method-function-python/
文章標籤
全站熱搜
留言列表