The relationship between @classmethod @staticmethod @property in Python2.7 learning

Source: Internet
Author: User

First come to an instance scene, then test, compare, do not understand the words to find information online:
#!/usr/bin/env python

#!/usr/bin/env python

Class Date (object):
def __init__ (self,year=0,month=0,day=0):
Self.year = Year
Self.month = Month
Self.day = Day


     @staticmethod
     def statictime (self):
          return ' {year}-{month}-{day} '. Format (
              year = self.year,
              month = self.month,
              day = self.day
        )

    @property
     def time (self):
          return ' {year}-{month}-{day} '. Format (
              year = self.year,
              month = Self.month,
             day = Self.day
        )
     @classmethod
     def from_string (cls,string):
         Year, Month,day = Map (Str,string.split ('-'))
         date = CLS (Year,month, Day)
         return date

def Showtime (self):
Return "{m}/{d}/{y}". Format (
Y = Self.year,
M = Self.month,
D = Self.day
)
print '-' *20
Date = Date ("2016", "11", "09")
print ' Date @property ', Date.time
print ' Date @normal ', Date.showtime ()
print ' Date @staticmethod ', Date.statictime (date)

print '-' *20
date_string = ' 2017-05-27 '
Year,month,day = Map (Str,date_string.split ('-'))
Date2 = Date (year,month,day)
print ' Date2 @property ', date2.time
print ' Date2 @noraml ', Date2.showtime ()
print ' Date2 @staticmethod ', Date2.statictime (date2)

print '-' *20
Date3 = date.from_string (date_string)
print ' Date3 @property ', date3.time
print ' Date3 @normal ', Date3.showtime ()
print ' Date3 @staticmethod ', Date3.statictime (Date3)

Operation Result:
--------------------
Date @property 2016-11-09
Date @normal 11/09/2016
Date @staticmethod 2016-11-09
--------------------
Date2 @property 2017-05-27
Date2 @noraml 05/27/2017
Date2 @staticmethod 2017-05-27
--------------------
Date3 @property 2017-05-27
Date3 @normal 05/27/2017
Date3 @staticmethod 2017-05-27


Preliminary test results:

1, @property the function behind the adorner, cannot be added () in the instance call, or an error will occur: TypeError: ' str ' object is not callable

2, the normal method, if not add () in the instance call, is not to run the function, but only return the function (method) address:

3, @staticmethod the function behind the adorner, you must add the image parameter in the instance call, otherwise you will be prompted: Typeerror:statictime () takes exactly 1 argument (0 given)

4, @classmethod My preliminary understanding: Is the class, the redefinition of the initialization.

========================================================

Example prototype Link: http://mp.weixin.qq.com/s/6NJAoFI-DbsNQdA3M1SIMw (very recommended for everyone to see)

Continue tomorrow ....

The relationship between @classmethod @staticmethod @property in Python2.7 learning

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.