引述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. 1.Method is called by its name, but it is associated to an object (dependent).
  2. 2.A method is implicitly passed the object on which it is invoked.
  3. 3.It may or may not return any data.
  4. 4.A method can operate on the data (instance variables) that is contained by the corresponding class

 

Basic Method Structure in Python :

p1.png

output

p2.png

Python 3 Inbuilt method :

p3.png

output

p4.png

 

 

 

 

 

Python Functions

  1. 1.Function is block of code that is also called by its name. (independent)
  2. 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. 3.It may or may not return any data.
  4. 4.Function does not deal with Class and its instance concept.

 

Python 3 User-Defined Function :

p5.png

Output

p6.png

Python 3 Inbuilt Function :

p7.png

 

參考文件:

https://www.geeksforgeeks.org/difference-method-function-python/

 

arrow
arrow

    阿丹 發表在 痞客邦 留言(0) 人氣()