对象到 Python 中的字符串
object to string in Python
我有一些数据对象,我想在这些对象上实现一个 to string 和 equals 深入的函数。
我实现了 str 和 eq,虽然平等工作正常但我不能让 str 表现同理:
class Bean(object):
def __init__(self, attr1, attr2):
self.attr1 = attr1
self.attr2 = attr2
def __str__(self):
return str(self.__dict__)
def __eq__(self, other):
return self.__dict__ == other.__dict__
当我运行:
t1 = Bean("bean 1", [Bean("bean 1.1", "same"), Bean("bean 1.2", 42)])
t2 = Bean("bean 1", [Bean("bean 1.1", "same"), Bean("bean 1.2", 42)])
t3 = Bean("bean 1", [Bean("bean 1.1", "different"), Bean("bean 1.2", 42)])
print(t1)
print(t2)
print(t3)
print(t1 == t2)
print(t1 == t3)
我得到:
{'attr2': [<__main__.Bean object at 0x7fc092030f28>, <__main__.Bean object at 0x7fc092030f60>], 'attr1': 'bean 1'}
{'attr2': [<__main__.Bean object at 0x7fc091faa588>, <__main__.Bean object at 0x7fc092045128>], 'attr1': 'bean 1'}
{'attr2': [<__main__.Bean object at 0x7fc0920355c0>, <__main__.Bean object at 0x7fc092035668>], 'attr1': 'bean 1'}
True
False
由于 t1 和 t2 包含相同的值,因此等于 return true(如预期),而由于 t3 在列表中包含不同的值,因此结果为 false(也如预期)。
我想要的是对 to 字符串具有相同的行为(基本上也对列表中的元素(或 set 或 dict ...)进行深入研究)。
对于 print(t1) 我想获得类似的东西:
{'attr2': ["{'attr2': 'same', 'attr1': 'bean 1.1'}", "{'attr2': 42, 'attr1': 'bean 1.2'}"], 'attr1': 'bean 1'}
如果我这样做实际上会得到:
Bean("bean 1", [Bean("bean 1.1", "same").__str__(), Bean("bean 1.2", 42).__str__()]).__str__
因为我不知道我的 Bean 对象中属性 attr1、attr2 的类型(它们可能是列表,但也可能是集合、字典等),如果有一个简单而优雅的解决方案,不需要类型检查 ...
这可能吗?
您可以使用 __repr__
而不是 __str__
,它递归地工作,尽管大多数时候这不是一个好主意(有关详细信息,请参阅 this 答案)。不过,这对我有用:
def __repr__(self):
return str(self.__dict__)
您可以尝试使用 repr 而不是 str。
当使用 def repr(self): 而不是 str 时,我得到了 print(t1) 的以下输出。
{'attr1': 'bean 1', 'attr2': [{'attr1': 'bean 1.1', 'attr2': 'same'}, {'attr1': 'bean 1.2', 'attr2': 42}]}
如果这能解决您的问题,请告诉我。附上image供参考
此致,
维尼思
我有一些数据对象,我想在这些对象上实现一个 to string 和 equals 深入的函数。
我实现了 str 和 eq,虽然平等工作正常但我不能让 str 表现同理:
class Bean(object):
def __init__(self, attr1, attr2):
self.attr1 = attr1
self.attr2 = attr2
def __str__(self):
return str(self.__dict__)
def __eq__(self, other):
return self.__dict__ == other.__dict__
当我运行:
t1 = Bean("bean 1", [Bean("bean 1.1", "same"), Bean("bean 1.2", 42)])
t2 = Bean("bean 1", [Bean("bean 1.1", "same"), Bean("bean 1.2", 42)])
t3 = Bean("bean 1", [Bean("bean 1.1", "different"), Bean("bean 1.2", 42)])
print(t1)
print(t2)
print(t3)
print(t1 == t2)
print(t1 == t3)
我得到:
{'attr2': [<__main__.Bean object at 0x7fc092030f28>, <__main__.Bean object at 0x7fc092030f60>], 'attr1': 'bean 1'}
{'attr2': [<__main__.Bean object at 0x7fc091faa588>, <__main__.Bean object at 0x7fc092045128>], 'attr1': 'bean 1'}
{'attr2': [<__main__.Bean object at 0x7fc0920355c0>, <__main__.Bean object at 0x7fc092035668>], 'attr1': 'bean 1'}
True
False
由于 t1 和 t2 包含相同的值,因此等于 return true(如预期),而由于 t3 在列表中包含不同的值,因此结果为 false(也如预期)。 我想要的是对 to 字符串具有相同的行为(基本上也对列表中的元素(或 set 或 dict ...)进行深入研究)。
对于 print(t1) 我想获得类似的东西:
{'attr2': ["{'attr2': 'same', 'attr1': 'bean 1.1'}", "{'attr2': 42, 'attr1': 'bean 1.2'}"], 'attr1': 'bean 1'}
如果我这样做实际上会得到:
Bean("bean 1", [Bean("bean 1.1", "same").__str__(), Bean("bean 1.2", 42).__str__()]).__str__
因为我不知道我的 Bean 对象中属性 attr1、attr2 的类型(它们可能是列表,但也可能是集合、字典等),如果有一个简单而优雅的解决方案,不需要类型检查 ...
这可能吗?
您可以使用 __repr__
而不是 __str__
,它递归地工作,尽管大多数时候这不是一个好主意(有关详细信息,请参阅 this 答案)。不过,这对我有用:
def __repr__(self):
return str(self.__dict__)
您可以尝试使用 repr 而不是 str。
当使用 def repr(self): 而不是 str 时,我得到了 print(t1) 的以下输出。
{'attr1': 'bean 1', 'attr2': [{'attr1': 'bean 1.1', 'attr2': 'same'}, {'attr1': 'bean 1.2', 'attr2': 42}]}
如果这能解决您的问题,请告诉我。附上image供参考
此致, 维尼思