Python @classmethod usage Scenarios

Source: Internet
Author: User
Tags date1 instance method

The official word for the use of Python @classmethod:
Classmethod (function)
English Description:
Classmethod is used to specify a method of a class as a class method, without this parameter specifying the class method as an instance method, using the following method:
Class C:    @classmethod    def f (CLS, arg1, Arg2, ...): ...


It's really confused after the look. What are you talking about???

Myself to the foreign forum to see other examples and explanations, immediately very clear. Let's use the examples below to illustrate them.

Look at the following definition of a time class:
Class Data_test (object):    day=0    month=0    year=0    def __init__ (self,year=0,month=0,day=0):        Self.day=day        self.month=month        self.year=year    def out_date (self):        print "year:"        print Self.year        print "month:"        print self.month        print "Day:"        print Self.day
T=data_test (2016,8,1) t.out_date ()

Output:
Year:2016month:8day:1

Meet expectations.

If the user enters a character format such as "2016-8-1", then it needs to be processed before calling the Date_test class:
String_date= ' 2016-8-1 ' Year,month,day=map (int,string_date.split ('-')) S=data_test (Year,month,day)

First break the ' 2016-8-1 ' into year,month,day three variables, then turn to int, and then call the Date_test (year,month,day) function. Also very well in line with expectations.

Can I put this string-handling function into the Date_test class?

So @classmethod's on the show.
Class Data_test2 (object):    day=0    month=0    year=0    def __init__ (self,year=0,month=0,day=0):        Self.day=day        self.month=month        self.year=year    @classmethod    def get_date (cls,string_date):        # The first parameter here is the CLS, which represents the invocation of the current class name        Year,month,day=map (int,string_date.split ('-'))        date1=cls (year,month,day)        #返回的是一个初始化后的类        return date1    def out_date (self):        print "year:"        print self.year        print "Month: "        print self.month        print" Day: "        print Self.day

Create a member function inside the Date_test class, preceded by a @classmethod decoration. It works a bit like a static class, unlike a static class, which can pass in a current class as the first parameter.

So how do you call it?
R=data_test2.get_date ("2016-8-6") r.out_date ()
Output:
Year:2016month:8day:1

This is equivalent to calling Get_date () to process the string before using Data_test's constructor to initialize.

The advantage is that you don't have to modify the constructor when you refactor the class, just add the extra functions you want to handle, and then use the adorner @classmethod.

Python @classmethod usage Scenarios

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.