- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想做这样的事情:
class X:
@classmethod
def id(cls):
return cls.__name__
def id(self):
return self.__class__.__name__
现在为类或其实例调用 id()
:
>>> X.id()
'X'
>>> X().id()
'X'
显然,这个确切的代码不起作用,但是否有类似的方法让它起作用?
或者在没有太多“hacky”东西的情况下获得这种行为的任何其他解决方法?
最佳答案
类和实例方法存在于同一个命名空间中,你不能重复使用这样的名称; id
的最后一个定义在这种情况下会赢。
类方法将继续在实例上工作,但是,不需要创建一个单独的实例方法;只需使用:
class X:
@classmethod
def id(cls):
return cls.__name__
因为方法继续绑定(bind)到类:
>>> class X:
... @classmethod
... def id(cls):
... return cls.__name__
...
>>> X.id()
'X'
>>> X().id()
'X'
这是明确记录的:
It can be called either on the class (such as
C.f()
) or on an instance (such asC().f()
). The instance is ignored except for its class.
如果您需要一种方法根据其使用位置不同来工作;在类上访问时绑定(bind)到类,在实例上访问时绑定(bind)到实例,您需要创建一个自定义的描述符对象。
descriptor API是 Python 如何将函数绑定(bind)为方法,并绑定(bind) classmethod
类的对象;查看descriptor howto .
您可以通过创建一个具有 __get__
的对象来为方法提供您自己的描述符方法。这是一个简单的方法,如果第一个参数为 __get__
,则根据上下文切换方法绑定(bind)的内容。是None
,则描述符被绑定(bind)到一个类,否则它被绑定(bind)到一个实例:
class class_or_instancemethod(classmethod):
def __get__(self, instance, type_):
descr_get = super().__get__ if instance is None else self.__func__.__get__
return descr_get(instance, type_)
这重新使用了 classmethod
并且只重新定义它如何处理绑定(bind),将原始实现委托(delegate)给 instance is None
, 和标准函数 __get__
否则实现。
请注意,在方法本身中,您可能需要测试它绑定(bind)的内容。 isinstance(firstargument, type)
是一个很好的测试:
>>> class X:
... @class_or_instancemethod
... def foo(self_or_cls):
... if isinstance(self_or_cls, type):
... return f"bound to the class, {self_or_cls}"
... else:
... return f"bound to the instance, {self_or_cls"
...
>>> X.foo()
"bound to the class, <class '__main__.X'>"
>>> X().foo()
'bound to the instance, <__main__.X object at 0x10ac7d580>'
另一种实现可以使用两个函数,一个用于绑定(bind)到一个类,另一个用于绑定(bind)到一个实例:
class hybridmethod:
def __init__(self, fclass, finstance=None, doc=None):
self.fclass = fclass
self.finstance = finstance
self.__doc__ = doc or fclass.__doc__
# support use on abstract base classes
self.__isabstractmethod__ = bool(
getattr(fclass, '__isabstractmethod__', False)
)
def classmethod(self, fclass):
return type(self)(fclass, self.finstance, None)
def instancemethod(self, finstance):
return type(self)(self.fclass, finstance, self.__doc__)
def __get__(self, instance, cls):
if instance is None or self.finstance is None:
# either bound to the class, or no instance method available
return self.fclass.__get__(cls, None)
return self.finstance.__get__(instance, cls)
这是一个带有可选实例方法的类方法。像使用 property
一样使用它目的;用 @<name>.instancemethod
装饰实例方法:
>>> class X:
... @hybridmethod
... def bar(cls):
... return f"bound to the class, {cls}"
... @bar.instancemethod
... def bar(self):
... return f"bound to the instance, {self}"
...
>>> X.bar()
"bound to the class, <class '__main__.X'>"
>>> X().bar()
'bound to the instance, <__main__.X object at 0x10a010f70>'
就我个人而言,我的建议是谨慎使用它;基于上下文改变行为的完全相同的方法使用起来可能会造成混淆。但是,有一些用例,例如 SQLAlchemy 对 SQL 对象和 SQL 值的区分,其中模型中的列对象会像这样切换行为;查看他们的 Hybrid Attributes documentation .此实现遵循与我的 hybridmethod
完全相同的模式。上面的类。
关于python - classmethod 和 instancemethod 同名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28237955/
我写了我遇到的问题,但现在我展示了所有代码xml文件 it can't contain other elements it can contain some other elements
我有两个 Controller using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using
我正在尝试保存几个同名的文件。我想做一些名字做这样的事情:file.extension file[1].extension file[2].extension 我试过这个 http://www.nas
我正在尝试创建一个脚本,该脚本将在多个页面上单击相同名称的按钮,但这些按钮具有不同的属性。有些有 id、一些名称、一些值和一些数据作为标识符。这个函数应该有一个属性列表,当我运行脚本时,它应该进入该列
从 Oracle 迁移我想重用一些定义,例如: ALTER TABLE CLIENTUSERS ADD CONSTRAINT UK_CLIENTUSERS_CLIENTUS UNIQUE (CLIEN
我有一个关于 unix 命令行的问题。我有很多这样的文件: /f/f1/file.txt /f/f2/file.txt /f/f3/file.txt and so on. 我想将所有 file.txt
我在表单“delsel2”中有几个复选框(全部具有相同的名称),如下所示: 我希望能够在选择其中任何一个时简单地发出警报,并显示一条消息指示是否选中其中任何一个。 我已经想出了这个,但它不起作用
假设您在子目录 project_1, ..., project_10 中有 10 个项目,它们都在父目录 projects 中。 所有项目都使用相同的库,在 project/project_n/lib
我有一个生成 iOS 框架的 Xcode 项目。我有两个版本的二进制文件要生成 - 一个供内部使用,没有许可要求,另一个供外部使用,但有许可要求。 我想我会为此制定两个目标,并使用不同的 swift
我已经读过有关 jQuery 验证的内容,但我仍然坚持在表单中需要控制数组。 我有这个代码: //already link the validation js script of course
我对这个领域很陌生。我正在尝试阅读一个示例程序。 第一个是team.c #include "support.h" struct team_t team = { "", /* first membe
我有一个包含一组复选框的 HTML 表单: Apple Banana Strawberry 当我单击一个按钮时,我希望将所有选中的同名复选框放在一个数组中。 因此,如果用户选择 Apple 和
我将第一个表存储在数据库中,如图所示,我必须对其应用查询以像第二个表一样显示它 最佳答案 这是使用条件聚合的一个选项: select subject, max(case when exa
对于我文件夹中的每个 mp3 文件,我都有一个同名的 jpg 文件,我试图用 cmd 实现的是将该名称变成一个没有文件扩展名的变量名 这是我只使用 mp3 名称的代码 for %%a in ("*.m
我有 2 个数据库....a 和 b 我在这两个数据库中都有表“t”。 现在我正在从数据库“b”中删除表 t。 我创建了一个 View “t”(请参阅 View 名称与已删除的表相同) 数据库“b
我正在创建一个有特定问题的 SCSS 网格 - 我想使用一个变量名,例如 $pad(用于填充值),但是 $pad变量在不同的媒体断点中需要不同。 首先通过动态创建断点并在其中设置 $pad 值的 mi
一些背景知识: 我正在尝试将我在 Solaris 上运行的一些 .ksh 文件移植到使用 Cygwin 的 Windows 上运行。在 Solaris 机器上运行的 ksh 实现与可在处理子 shel
当执行 tar 提取操作时,有时内容会直接提取到父文件夹,这很糟糕,因为它们变得杂乱无章。例如: tar -xzf foo1.tar.gz 摘录: ./file1 ./file2 ./file3 解决
我想做这样的事情: class X: @classmethod def id(cls): return cls.__name__ def id(self):
我正在看 John De Goes “FP to the Max”视频。在代码中,他做了这样的事情来获取隐式对象: object Program { def apply[F[_]](imp
我是一名优秀的程序员,十分优秀!