Overview
Recently working on a project: the CMDB (Configuration Management Database) asset acquisition, which involves a Classmethod (class method) approach, is not found.
There is also a Staticmethod (static method) method has been unfamiliar, so from the online access to information, strengthen understanding, and then collated as follows:
@classmethod: Class methods (methods that run only in the class but not in the instance)
@staticmethod often have some functionality that is related to the class, but static methods are required when the runtime does not require instances and classes to participate.
.such as changing the environment variables or modifying the properties of other classes can be used to static methods
.This can be solved directly with a function, but it also spreads the code inside the class, causing maintenance difficulties
.
start with the code belowThe official argument for Classmethod is to specify a class method as a class method, without this parameter specifying the method of the class as an instance method, using the following method:
Class C: @classmethod def f (cls,arg1,arg2,...):
For example, let's be clear.
Look at one of the time classes defined below:
Class Date_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:%s;month:%s;day:%s "% (Self.year,self.month,self.day)) t=date_test (2016,8,1) t.out_date () operation Result: Year:2016;month:8;day:1
Meet expectations.
If the user enters the format "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=date_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?
Then @classmethod began to play.
Class Date_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 @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:%s;month:%s;day:%s"% (Self.year, Self.month,self.day))
Operation Result:
Year:2016;month:8;day:1
Is the moment very clear?!
Let's look at the usage of Staticmethod and the difference between the two.
Scenario: When writing a class, there are many different ways to create instances, and we have only one __init__ function, where static methods come into use.
The following code:
Import Timeclass Date (object): def __init__ (self,year,month,day): self.year=year Self.month=month self.day=day @staticmethod def now (): t=time.localtime () return Date (T.tm_year, T.tm_mon , T.tm_mday) @staticmethod def tomorrow (): t=time.localtime (Time.time () +86400) Return Date (T.tm_year,t.tm_mon,t.tm_mday)a=date (2017,9,29) B=date.now () C=date.tomorrow () print (' __init__ method: ', a.year,a.month,a.day) print (' Staticmethod method, Current time:%s-%s-%s '% (b.year,b.month,b.day)) print (' Staticmethod method, tomorrow time: %s-%s-%s '% (c.year,c.month,c.day)) operation Result: Staticmethod method, Current time: 2017-9-29staticmethod method, tomorrow time: 2017-9-30
Tips
Python represents time in three ways: 1) timestamp 2) formatted time string 3) tuple (struct_time)
Tuple (struct_time) mode: There are 9 elements in the Struct_time tuple, and the functions that return struct_time are mainly gmtime (), localtime (), Strptime (). Several elements in this way tuple are listed below:
Common time Functions:
(1) Time.localtime ([secs]): Converts a timestamp to the struct_time of the current time zone. The secs parameter is not provided, whichever is the current time.
(2) Time.gmtime ([secs]): Similar to the LocalTime () method, the Gmtime () method is the struct_time of converting a timestamp to the UTC time zone (0 o'clock Zone).
(3) Time.strptime (string[, format]): Converts a formatted time string to Struct_time. In fact it is inverse operation with strftime ().
Follow-up, check leaks fill, to be supplemented.
In terms of recent CMDB projects encountered problems collation: Class method: Classmethod, Staticmethod