Python - 靜態函數(staticmethod), 類函數(classmethod), 成員函數 區別(完全解析)

來源:互聯網
上載者:User

標籤:comm   net   keyword   foo   pydev   ati   def   輸出   flow   

原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995

靜態函數(staticmethod), 類函數(classmethod), 成員函數的區別(完全解析)

 

定義:

靜態函數(@staticmethod): 即靜態方法,主要處理與這個類的邏輯關聯, 如驗證資料;

類函數(@classmethod):即類方法, 更關注於從類中調用方法, 而不是在執行個體中調用方法, 如構造重載;

成員函數: 執行個體的方法, 只能通過執行個體進行調用;

代碼:

 

# -*- coding: utf-8 -*-    #eclipse pydev, python 3.3  #by C.L.Wang    class A(object):            _g = 1            def foo(self,x):          print(‘executing foo(%s,%s)‘%(self,x))       @classmethod      def class_foo(cls,x):          print(‘executing class_foo(%s,%s)‘%(cls,x))       @staticmethod      def static_foo(x):          print(‘executing static_foo(%s)‘%x)       a = A()  a.foo(1)  a.class_foo(1)  A.class_foo(1)  a.static_foo(1)  A.static_foo(‘hi‘)  print(a.foo)  print(a.class_foo)  print(a.static_foo)  

 

 

輸出:

 

 

executing foo(<__main__.A object at 0x01E2E1B0>,1)  executing class_foo(<class ‘__main__.A‘>,1)  executing class_foo(<class ‘__main__.A‘>,1)  executing static_foo(1)  executing static_foo(hi)  <bound method A.foo of <__main__.A object at 0x01E2E1B0>>  <bound method type.class_foo of <class ‘__main__.A‘>>  <function A.static_foo at 0x01E7E618>  

 


具體應用:

 

 

比如日期的方法, 可以通過執行個體化(__init__)進行資料輸出;

可以通過類方法(@classmethod)進行資料轉換;

可以通過靜態方法(@staticmethod)進行資料驗證;

 

代碼:

 

  1. # -*- coding: utf-8 -*-  
  2.   
  3. #eclipse pydev, python 3.3  
  4. #by C.L.Wang  
  5.   
  6. class Date(object):  
  7.   
  8.     day = 0  
  9.     month = 0  
  10.     year = 0  
  11.   
  12.     def __init__(self, day=0, month=0, year=0):  
  13.         self.day = day  
  14.         self.month = month  
  15.         self.year = year  
  16.           
  17.     def display(self):  
  18.         return "{0}*{1}*{2}".format(self.day, self.month, self.year)  
  19.      
  20.     @classmethod  
  21.     def from_string(cls, date_as_string):  
  22.         day, month, year = map(int, date_as_string.split(‘-‘))  
  23.         date1 = cls(day, month, year)  
  24.         return date1  
  25.      
  26.     @staticmethod  
  27.     def is_date_valid(date_as_string):  
  28.         day, month, year = map(int, date_as_string.split(‘-‘))  
  29.         return day <= 31 and month <= 12 and year <= 3999  
  30.       
  31. date1 = Date(‘12‘, ‘11‘, ‘2014‘)  
  32. date2 = Date.from_string(‘11-13-2014‘)  
  33. print(date1.display())  
  34. print(date2.display())  
  35. print(date2.is_date_valid(‘11-13-2014‘))  
  36. print(Date.is_date_valid(‘11-13-2014‘))  

 


輸出:

 

 

12*11*2014  11*13*2014  False  False  

 

 


參考:

 

http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner

http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python

Python - 靜態函數(staticmethod), 類函數(classmethod), 成員函數 區別(完全解析)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.