python类方法
Python classmethod is the way to define a function for the python class.
Python类方法是为python类定义函数的方法。
Python类方法 (Python classmethod)
In this post on Python, we will see how we can define and use class methods. We will see ways in which we can create class methods and call them in our program.
在有关Python的这篇文章中,我们将看到如何定义和使用类方法。 我们将看到创建类方法并在程序中调用它们的方法。
Python中的方法类型 (Type of methods in Python)
In Python, there are two types of functions which can be created in a program. They are:
在Python中,可以在程序中创建两种类型的函数。 他们是:
- Static methods 静态方法
- Class methods 类方法
On how to create and use static methods, read this post.
有关如何创建和使用静态方法的信息,请阅读这篇文章 。
In this lesson, we will focus on class methods.
在本课程中,我们将重点介绍类方法。
制作python类方法 (Making python class methods)
A class method is the one which can be called only with the instance of an Object. These methods usually define the behavior of an object and modify the object’s properties or instance variables.
类方法是只能用Object实例调用的方法。 这些方法通常定义对象的行为并修改对象的属性或实例变量。
There are two ways to create class methods in Python:
有两种方法可以在Python中创建类方法:
- Using classmethod(function) 使用classmethod(function)
- Using @classmethod annotation 使用@classmethod注释
Using classmethod()
look non-Pythonic and hence, in newer Python versions, we can use the @classmethod annotation decorator for making class methods.
使用classmethod()
看起来不是Python风格的,因此,在较新的Python版本中,我们可以使用@classmethod批注装饰器来制作类方法。
提供样板 (Providing Boilerplate)
In this section, we will try to establish an understanding with class methods. Here is a sample program to start with:
在本节中,我们将尝试建立对类方法的理解。 这是一个示例程序,以开始:
class DateTime(object):
def __init__(self, day=10, month=10, year=2000):
self.day = day
self.month = month
self.year = year
This is just a simple class definition to start with. We will build our example on this. We included an __init__
function to initialize the instance of the object for this class.
这只是一个简单的类定义。 我们将以此为基础。 我们包含了一个__init__
函数来初始化此类的对象实例。
包括类方法 (Including class methods)
Now, we will enhance the boilerplate code in the last section and include a class method in it which can receive a date as a String
and return a real Date
instance. Let’s look at a code snippet:
现在,我们将在最后一部分中增强样板代码,并在其中包括一个类方法,该方法可以将日期作为String
接收并返回实际的Date
实例。 让我们看一下代码片段:
@classmethod
def from_string(cls, string_date):
day, month, year = map(int, string_date.split('-'))
myDate = cls(day, month, year)
return myDate
Notice that this method will act as a constructor for this class as well due to the reason that this method takes in class as its reference. Also, it actually constructs the class instance from a String.
注意,由于该方法将类作为其引用的原因,该方法也将充当此类的构造函数 。 而且,它实际上是从字符串构造类实例。
Let’s look at a code snippet on how this constructor can be put to use:
让我们看一下如何使用此构造函数的代码片段:
dateObj = DateTime.from_string('20-05-1994')
它与静态方法有何不同? (How does it differ from static method?)
Static methods are the one which belongs to a class rather than a particular instance of that class. Here is a sample static method for the DateTime
class we defined::
静态方法是属于一类的方法,而不是该类的特定实例。 这是我们定义的DateTime
类的示例静态方法:
@staticmethod
def is_valid_date(date_as_string):
day, month, year = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
This can be put to use like:
可以像这样使用:
is_valid_date = Date.is_date_valid('20-05-1994')
Here, we don’t do anything with any instance of the class and just check if something is appropriate to be converted to the instance of that class.
在这里,我们对类的任何实例不做任何事情,只是检查是否有适当的东西可以转换为该类的实例。
Our complete class code looks like:
我们完整的类代码如下:
class DateTime(object):
def __init__(self, day=10, month=10, year=2000):
self.day = day
self.month = month
self.year = year
@classmethod
def from_string(cls, string_date):
day, month, year = map(int, string_date.split('-'))
myDate = cls(day, month, year)
return myDate
@staticmethod
def is_valid_date(date_as_string):
day, month, year = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
dateObj = DateTime.from_string('20-05-1994')
is_valid_date = DateTime.is_valid_date('20-05-1994')
print(is_valid_date)
Output will look simply:
![python classmethod example](https://cdn.journaldev.com/wp-content/uploads/2018/02/python-classmethod.png)
输出看起来很简单:
结论 (Conclusion)
The @classmethod
annotation decorator is used to create factory methods as they can take any input and provide a class object based on the parameters and processing. Using this decorator, it is possible to create as many constructors for a class which behaves as factory constructors.
@classmethod
批注装饰器用于创建工厂方法,因为它们可以接受任何输入并根据参数和处理提供类对象。 使用此装饰器,可以为一个行为与工厂构造函数相同的类创建尽可能多的构造函数。
python类方法