類 python,python類方法_Python類方法

 2023-11-19 阅读 35 评论 0

摘要: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 i

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的這篇文章中,我們將看到如何定義和使用類方法。 我們將看到創建類方法并在程序中調用它們的方法。

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.

有關如何創建和使用靜態方法的信息,請閱讀這篇文章 。

Python中的類、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中類方法、 有兩種方法可以在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 = 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__函數來初始化此類的對象實例。

包括類方法 (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.

python方法, 注意,由于該方法將類作為其引用的原因,該方法也將充當此類的構造函數 。 而且,它實際上是從字符串構造類實例。

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

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:

python classmethod example

輸出看起來很簡單:

結論 (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類的調用方法、翻譯自: https://www.journaldev.com/18981/python-classmethod

python類方法

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/183173.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息