I still believe this question has a high probability of being an XY problem.
However, regarding your question as it stands: If you don't want to modify print
, you're only left with modifying the return value of your function a
. Your comment above says:
I want to return a dict
This sounds like simply returning a modified string representation of {'row_contents': content}
isn't really what you're looking for. Yet, dict.__repr__
itself is read-only, so I guess the closest solution would be to return an instance of a custom dict
subclass:
class CustomDict(dict):
def __repr__(self):
return "{" + ", ".join([repr(k) + ": " + str(v) for k, v in self.items()]) + "}"
def a(content):
return CustomDict({'row_contents': content})
print(a("Hello"))
print(isinstance(a("Hello"), dict))
Which prints:
{'row_contents': Hello}
True
You might need to improve CustomDict.__repr__
depending on what modifications are necessary to provide the desired output. You could also modify the original string representation super().__repr__()
.