python類方法
Python classmethod is the way to define a function for the python class.
Python類方法是為python類定義函數的方法。
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的這篇文章中,我們將看到如何定義和使用類方法。 我們將看到創建類方法并在程序中調用它們的方法。
In Python, there are two types of functions which can be created in a program. They are:
在Python中,可以在程序中創建兩種類型的函數。 他們是:
On how to create and use static methods, read this post.
有關如何創建和使用靜態方法的信息,請閱讀這篇文章 。
Python中的類、In this lesson, we will focus on 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中類方法、 有兩種方法可以在Python中創建類方法:
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批注裝飾器來制作類方法。
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 = dayself.month = monthself.year = year
python爬蟲教程?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__
函數來初始化此類的對象實例。
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.
python方法, 注意,由于該方法將類作為其引用的原因,該方法也將充當此類的構造函數 。 而且,它實際上是從字符串構造類實例。
Let’s look at a code snippet on how this constructor can be put to use:
讓我們看一下如何使用此構造函數的代碼片段:
dateObj = DateTime.from_string('20-05-1994')
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
python有什么用。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:
python3? 我們完整的類代碼如下:
class DateTime(object):def __init__(self, day=10, month=10, year=2000):self.day = dayself.month = monthself.year = year@classmethoddef from_string(cls, string_date):day, month, year = map(int, string_date.split('-'))myDate = cls(day, month, year)return myDate@staticmethoddef is_valid_date(date_as_string):day, month, year = map(int, date_as_string.split('-'))return day <= 31 and month <= 12 and year <= 3999dateObj = DateTime.from_string('20-05-1994')
is_valid_date = DateTime.is_valid_date('20-05-1994')
print(is_valid_date)
Output will look simply:
輸出看起來很簡單:
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類的調用方法、翻譯自: https://www.journaldev.com/18981/python-classmethod
python類方法
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态