python 的@staticmethod 和 @classmethod

Difference between @staticmethod and @classmethod in Python - Python Central
http://pythoncentral.io/difference-between-staticmethod-and-classmethod-in-python/

 

Though classmethod and staticmethod are quite similar, there's a slight difference in usage for both entities: classmethod must have a reference to a class object as the first parameter, whereas staticmethod can have no parameters at all.

Let's look at all that was said in real examples.

Boilerplate

Let's assume an example of a class, dealing with date information (this is what will be our boilerplate to cook on):

 1 class Date(object):
 2 
 3     day = 0
 4     month = 0
 5     year = 0
 6 
 7     def __init__(self, day=0, month=0, year=0):
 8         self.day = day
 9         self.month = month
10         self.year = year

 

This class obviously could be used to store information about certain dates (without timezone information; let's assume all dates are presented in UTC).

Here we have __init__, a typical initializer of Python class instances, which receives arguments as a typical instancemethod, having the first non-optional argument (self) that holds reference to a newly created instance.

Class Method

We have some tasks that can be nicely done using classmethods.

Let's assume that we want to create a lot of Date class instances having date information coming from outer source encoded as a string of next format ('dd-mm-yyyy'). We have to do that in different places of our source code in project.

So what we must do here is:

  1. Parse a string to receive day, month and year as three integer variables or a 3-item tuple consisting of that variable.
  2. Instantiate Date by passing those values to initialization call.

This will look like:

1 day, month, year = map(int, string_date.split('-'))
2 date1 = Date(day, month, year)

 

For this purpose, C++ has such feature as overloading, but Python lacks that feature- so here's whenclassmethod applies. Lets create another "constructor".

1  @classmethod
2     def from_string(cls, date_as_string):   #第一个参数传入的是类,而不是实例,可以被子类继承。该方法可以对类本身的属性和方法进行操作.
3         day, month, year = map(int, date_as_string.split('-'))
4         date1 = cls(day, month, year)
5         return date1
6 
7  date2 = Date.from_string('11-09-2012')

 

 

 

Let's look more carefully at the above implementation, and review what advantages we have here:

  1. We've implemented date string parsing in one place and it's reusable now.
  2. Encapsulation works fine here (if you think that you could implement string parsing as a single function elsewhere, this solution fits OOP paradigm far better).
  3. cls is an object that holds class itself, not an instance of the class. It's pretty cool because if we inherit our Date class, all children will have from_string defined also.

Static method

What about staticmethod? It's pretty similar to classmethod but doesn't take any obligatory parameters (like a class method or instance method does).

Let's look at the next use case.

We have a date string that we want to validate somehow. This task is also logically bound to Dateclass we've used so far, but still doesn't require instantiation of it.

Here is where staticmethod can be useful. Let's look at the next piece of code:

1 @staticmethod
2     def is_date_valid(date_as_string):          #相当于一个写在类里面的外部函数,不用给该函数传递类或实例,不能被子类继承
3         day, month, year = map(int, date_as_string.split('-'))
4         return day <= 31 and month <= 12 and year <= 3999
5 
6  # usage:
7  is_date = Date.is_date_valid('11-09-2012')

 

So, as we can see from usage of staticmethod, we don't have any access to what the class is- it's basically just a function, called syntactically like a method, but without access to the object and it's internals (fields and another methods), while classmethod does.

 

 

 

@classmethod means: when this method is called, we pass the class as the first argument instead of the instance of that class (as we normally do with methods). This means you can use the class and its properties inside that method rather than a particular instance.

@staticmethod means: when this method is called, we don't pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can't access the instance of that class (this is useful when your method does not use the instance).

posted @ 2016-08-21 10:16  hb91  阅读(240)  评论(0编辑  收藏  举报