Python-static function (Staticmethod), class function (Classmethod), member function difference (full parse)

Source: Internet
Author: User
Tags date1

Original address: http://blog.csdn.net/caroline_wendy/article/details/23383995

static functions (Staticmethod), class functions (Classmethod), differences in member functions (fully parsed)

Defined:

static function (@staticmethod): A static method that deals primarily with the logical association of this class, such as validating data;

class function (@classmethod): A class method that focuses more on invoking a method from a class than invoking a method in an instance, such as constructing an overload;

member function: The method of the instance, can only be called through the instance;

Code:

#-*-coding:utf-8-*-  #Eclipse Pydev, Python 3.3#by C.l.wang  classA (object): _g= 1deffoo (self,x):Print('executing foo (%s,%s)'%(self,x)) @classmethoddefClass_foo (cls,x):Print('executing Class_foo (%s,%s)'%(cls,x)) @staticmethoddefStatic_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)

Output:

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>


Specific application:

For example, the date method can be instantiated (__init__) for data output;

Data conversion can be done through the class method (@classmethod) ;

Data validation can be performed by static methods (@staticmethod) ;

Code:

  1. #-*-Coding:utf-8-*-
  2. #eclipse Pydev, Python 3.3
  3. #by C.l.wang
  4. Class Date (object):
  5. Day = 0
  6. month = 0
  7. Year = 0
  8. def __init__ (self, day=0, month=0, year=0):
  9. Self.day = Day
  10. self.month = Month
  11. Self.year = Year
  12. def display (self):
  13. return "{0}*{1}*{2}". Format (self.day, self.month, self.year)
  14. @classmethod
  15. def from_string (cls, date_as_string):
  16. Day, month, year = map (int, date_as_string.split ('-'))
  17. Date1 = CLS (Day, month, year)
  18. return date1
  19. @staticmethod
  20. def is_date_valid (date_as_string):
  21. Day, month, year = map (int, date_as_string.split ('-'))
  22. return Day <= and month <= <= 3999
  23. Date1 = Date (' one ', ' one ', ' all ')
  24. Date2 = date.from_string (' 11-13-2014 ')
  25. Print (Date1.display ())
  26. Print (Date2.display ())
  27. Print (Date2.is_date_valid (' 11-13-2014 '))
  28. Print (Date.is_date_valid (' 11-13-2014 '))


Output:

12*11*2014  11*13*2014  false  false  


Reference:

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-static function (Staticmethod), class function (Classmethod), member function difference (full parse)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.