我希望能够更改 __str__()
的输出,并希望为此添加 **kwargs
。
问题:似乎无法更改函数的签名。
例如:
#!/usr/bin/env python3
class my_printable_obj(object):
def __init__(self, s):
self.content = s
def __str__(self, **kwargs):
fancy = kwargs.get('fancy', '')
return str(self.content) + fancy
M = my_printable_obj("Something to print")
print(str(M))
print(str(M, fancy=' + a fancy part'))
输出:
$ ./test_script3str.py
Something to print
Traceback (most recent call last):
File "./test_script3str.py", line 17, in <module>
print(str(M, fancy=' + a fancy part'))
TypeError: 'fancy' is an invalid keyword argument for this function
我注意到,尽管有 TypeError 消息,python 确实使用了 my_printable_obj.__str__()
(在 __str__ 的开头添加了一个
让我们很容易地观察到这一点。print('here')
()
是否有解决方案,或者由于某种原因无法解决?然后我应该更好地定义另一个函数,比如 into_str(self, **kwargs)
并使用 M.into_str(fancy='something more')
而不是 __str__()
?
注意:这样写:
def __str__(self, fancy=''):
return str(self.content) + fancy
给出完全相同的错误。
最佳答案
您使用 kwarg 调用的函数是 str
(the inbuilt function) .
str()
会在您的对象上调用 __str__
方法。
也许你要找的是__format__
>>> class MyPrintableObj(object):
def __init__(self, s):
self.content = s
def __format__(self, formatstr):
return str(self.content) + formatstr
>>> m = MyPrintableObj("Something to print")
>>> '{: a fancy string}'.format(m)
'Something to print a fancy string'
关于python - 有什么方法可以将 **kwargs 添加到 __str__() 的对象签名中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37616746/