- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在我正在处理的问题中,数据标识符的形式为 scope:name
,既是 scope
又是 name
字符串. name
由点分隔的不同部分,如 part1.part2.part3.part4.part5
。在很多情况下,但并非总是如此,scope
等于 name
的 part1
。我正在编写的代码必须与提供或需要不同模式标识符的不同系统一起使用。有时他们只需要完整的字符串表示形式,如 scope:name
,在其他一些情况下调用有两个不同的参数 scope
和 name
。当从其他系统接收信息时,有时返回完整的字符串 scope:name
,有时 scope
被省略,应该从 name
推断,有时返回包含 scope
和 name
的字典。
为了简化这些标识符的使用,我创建了一个类来在内部管理它们,这样我就不必一遍又一遍地编写相同的转换、拆分和格式。类(class)很简单。它只有两个属性(scope
和name
,一个将字符串解析为类对象的方法,以及一些表示对象的魔术方法特别是,__str__( self)
以 scope:name
形式返回对象,它是标识符的完全限定名 (fqn):
class DID(object):
"""Represent a data identifier."""
def __init__(self, scope, name):
self.scope = scope
self.name = name
@classmethod
def parse(cls, s, auto_scope=False):
"""Create a DID object given its string representation.
Parameters
----------
s : str
The string, i.e. 'scope:name', or 'name' if auto_scope is True.
auto_scope : bool, optional
If True, and when no scope is provided, the scope will be set to
the projectname. Default False.
Returns
-------
DID
The DID object that represents the given fully qualified name.
"""
if isinstance(s, basestring):
arr = s.split(':', 2)
else:
raise TypeError('string expected.')
if len(arr) == 1:
if auto_scope:
return cls(s.split('.', 1)[0], s)
else:
raise ValueError(
"Expecting 'scope:name' when auto_scope is False"
)
elif len(arr) == 2:
return cls(*arr)
else:
raise ValueError("Too many ':'")
def __repr__(self):
return "DID(scope='{0.scope}', name='{0.name}')".format(self)
def __str__(self):
return u'{0.scope}:{0.name}'.format(self)
正如我所说,代码必须执行与字符串的比较并使用某些方法的字符串表示。我很想编写 __eq__
魔术方法及其对应的 __ne__
。以下是 __eq__
的实现:
# APPROACH 1:
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.scope == other.scope and self.name == other.name
elif isinstance(other, basestring):
return str(self) == other
else:
return False
如您所见,它以一种可以相互比较的方式定义了 DID 和字符串之间的相等性比较。 我的问题是这是否是一种好的做法:
一方面,当 other
是一个字符串时,该方法将 self
转换为一个字符串,我一直在思考显式优于隐式 em>。您最终可能会认为您正在使用两个字符串,而 self 不是这种情况。
另一方面,从意义的角度来看,DID
代表 fqn scope:name
并且与字符串比较是否相等是有意义的,因为它在比较 int 和 float 或比较从 basetring
派生的任何两个对象时执行。
我也考虑过在实现中不包括 basestring 的情况,但对我来说这更糟并且容易出错:
# APPROACH 2:
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.scope == other.scope and self.name == other.name
else:
return False
在方法 2 中,比较 DID 对象和字符串之间的相等性,两者都表示相同的标识符,返回 False
。对我来说,这更容易出错。
在这种情况下,最佳做法是什么?是否应该像方法 1 中那样实现 DID 和字符串之间的比较,即使来自不同类型的对象可能被认为是相等的?即使 s != DID.parse(s)
我也应该使用方法 2 吗?我不应该实现 __eq__
和 __ne__
以便永远不会被误解吗?
最佳答案
Python 中的几个类(但我想不出标准库中的任何东西)定义了一个处理 RHS 上多种类型的相等运算符。一个支持这一点的通用库是 NumPy,其中:
import numpy as np
np.array(1) == 1
评估为 True
。总的来说,我认为我不鼓励这种事情,因为在很多极端情况下,这种行为可能会变得棘手。例如。请参阅 Python 3 中的文章 __hash__
方法(类似的东西存在于 Python 2 中,但它已经过时了)。在我编写过类似代码的情况下,我往往会得到更接近于以下内容的代码:
def __eq__(self, other):
if isinstance(other, str):
try:
other = self.parse(str)
except ValueError:
return NotImplemented
if isinstance(other, DID):
return self.scope == other.scope and self.name == other.name
return NotImplemented
除此之外,我建议将此类对象设置为不可变对象(immutable对象),您可以通过多种方式实现。 Python 3 有不错的 dataclasses ,但鉴于您似乎被困在 Python 2 下,您可能会使用 namedtuple
,例如:
from collections import namedtuple
class DID(namedtuple('DID', ('scope', 'name'))):
__slots__ = ()
@classmethod
def parse(cls, s, auto_scope=False):
return cls('foo', 'bar')
def __eq__(self, other):
if isinstance(other, str):
try:
other = self.parse(str)
except ValueError:
return NotImplemented
return super(DID, self).__eq__(other)
它免费为您提供不变性和 repr 方法,但您可能希望保留自己的 str 方法。 __slots__
属性意味着意外分配给 obj.scopes
会失败,但您可能希望允许这种行为。
关于python - __eq__ 应该比较两种不同类型的对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57143808/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!