@classmethod
@staticmethod
据我了解,告诉一个类它是一个应该继承到子类的方法,或者……东西。但是,这有什么意义呢?为什么不只定义类方法而不添加 or 或任何定义?@classmethod
@classmethod
@staticmethod
@
网友回答:
罗斯蒂斯拉夫·津科的回答非常恰当。我想我可以强调在创建其他构造函数时应该选择的另一个原因。@classmethod
@staticmethod
在该示例中,Rostyslav 使用 as 工厂从其他不可接受的参数创建对象。也可以完成相同的操作,如下面的代码所示:@classmethod
from_string
Date
@staticmethod
class Date:
def __init__(self, month, day, year):
self.month = month
self.day = day
self.year = year
def display(self):
return "{0}-{1}-{2}".format(self.month, self.day, self.year)
@staticmethod
def millenium(month, day):
return Date(month, day, 2000)
new_year = Date(1, 1, 2013) # Creates a new Date object
millenium_new_year = Date.millenium(1, 1) # also creates a Date object.
# Proof:
new_year.display() # "1-1-2013"
millenium_new_year.display() # "1-1-2000"
isinstance(new_year, Date) # True
isinstance(millenium_new_year, Date) # True
因此,两者都是类的实例。new_year
millenium_new_year
Date
但是,如果您仔细观察,工厂进程是硬编码的,无论如何都可以创建对象。这意味着即使类是子类化的,子类仍将创建普通对象(没有任何子类的属性)。请参阅以下示例:Date
Date
Date
class DateTime(Date):
def display(self):
return "{0}-{1}-{2} - 00:00:00PM".format(self.month, self.day, self.year)
datetime1 = DateTime(10, 10, 1990)
datetime2 = DateTime.millenium(10, 10)
isinstance(datetime1, DateTime) # True
isinstance(datetime2, DateTime) # False
datetime1.display() # returns "10-10-1990 - 00:00:00PM"
datetime2.display() # returns "10-10-2000" because it's not a DateTime object but a Date object. Check the implementation of the millenium method on the Date class for more details.
datetime2
不是 的实例?跆拳道?嗯,那是因为使用的装饰器。DateTime
@staticmethod
在大多数情况下,这是不希望的。如果你想要的是一个知道调用它的类的工厂方法,那么这就是你需要的。@classmethod
重写为(这是上述代码中唯一更改的部分):Date.millenium
@classmethod
def millenium(cls, month, day):
return cls(month, day, 2000)
确保 不是硬编码而是学习的。 可以是任何子类。结果将正确地成为 的实例。
让我们测试一下:class
cls
object
cls
datetime1 = DateTime(10, 10, 1990)
datetime2 = DateTime.millenium(10, 10)
isinstance(datetime1, DateTime) # True
isinstance(datetime2, DateTime) # True
datetime1.display() # "10-10-1990 - 00:00:00PM"
datetime2.display() # "10-10-2000 - 00:00:00PM"
原因是,正如您现在所知,它被使用而不是@classmethod
@staticmethod
网友回答:
虽然 和 非常相似,但两个实体的用法略有不同:必须对类对象的引用作为第一个参数,而可以根本没有参数。classmethod
staticmethod
classmethod
staticmethod
class Date(object):
def __init__(self, day=0, month=0, year=0):
self.day = day
self.month = month
self.year = year
@classmethod
def from_string(cls, date_as_string):
day, month, year = map(int, date_as_string.split('-'))
date1 = cls(day, month, year)
return date1
@staticmethod
def is_date_valid(date_as_string):
day, month, year = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
date2 = Date.from_string('11-09-2012')
is_date = Date.is_date_valid('11-09-2012')
让我们假设一个处理日期信息的类示例(这将是我们的样板):
class Date(object):
def __init__(self, day=0, month=0, year=0):
self.day = day
self.month = month
self.year = year
这个类显然可以用来存储有关某些日期的信息(没有时区信息;假设所有日期都以UTC显示)。
这里有 ,Python 类实例的典型初始值设定项,它接收参数作为典型的实例方法,具有第一个非可选参数 (),其中包含对新创建的实例的引用。__init__
self
类方法
我们有一些可以使用 s 很好地完成的任务。classmethod
假设我们要创建许多 Date
类实例,这些实例的日期信息来自编码为格式为“dd-mm-yyyy”的字符串的外部源。假设我们必须在项目源代码的不同位置执行此操作。
所以我们在这里必须做的是:
Date
这将看起来像:
day, month, year = map(int, string_date.split('-'))
date1 = Date(day, month, year)
为此,C++可以通过重载来实现这样的功能,但 Python 缺少这种重载。相反,我们可以使用 .让我们创建另一个构造函数。classmethod
@classmethod
def from_string(cls, date_as_string):
day, month, year = map(int, date_as_string.split('-'))
date1 = cls(day, month, year)
return date1
date2 = Date.from_string('11-09-2012')
让我们更仔细地看一下上面的实现,并回顾一下我们在这里有什么优势:
cls
是类本身,而不是类的实例。这很酷,因为如果我们继承我们的类,所有的孩子也会定义。Date
from_string
静态方法
怎么样?它与类方法或实例方法非常相似,但不接受任何强制性参数(如类方法或实例方法)。staticmethod
classmethod
让我们看一下下一个用例。
我们有一个想要以某种方式验证的日期字符串。此任务在逻辑上也绑定到我们目前使用的 Date
类,但不需要实例化它。
这是有用的地方。让我们看下一段代码:staticmethod
@staticmethod
def is_date_valid(date_as_string):
day, month, year = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
# usage:
is_date = Date.is_date_valid('11-09-2012')
因此,正如我们从 的用法中看到的那样,我们无法访问类是什么 – 它基本上只是一个函数,在语法上像方法一样调用,但无法访问对象及其内部(字段和其他方法),而它确实有。staticmethod
classmethod
网友回答:
@classmethod
意味着:当调用此方法时,我们将类作为第一个参数传递,而不是该类的实例(就像我们通常使用方法所做的那样)。这意味着您可以在该方法而不是特定实例中使用类及其属性。
@staticmethod
意味着:当调用此方法时,我们不会将类的实例传递给它(就像我们通常对方法所做的那样)。这意味着您可以将函数放在类中,但无法访问该类的实例(当您的方法不使用实例时,这很有用)。
模板简介:该模板名称为【在 Python 中是什么意思,它们有什么不同?如何使用它们?@classmethod@staticmethod】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【Python】栏目查找您需要的精美模板。