Код: Выделить всё
class ILookDifferentEveryRun:
"baseline behavior"
def __init__(self,a):
"I don't actually care about `a`, that's why I don't need a `repr`"
self.a = a
class ILookTheSameEveryRun(ILookDifferentEveryRun):
"""this is my workaround, a cut and paste of a default __repr__"""
def __repr__(self) : return type(self).__name__
class ILookAlmostLikeBuiltinRepr(ILookDifferentEveryRun):
"can I do this with a global switch?"
def __repr__(self) :
"""this is more or less what I want"""
res = f" at "
return res
inst1 = ILookDifferentEveryRun(a=1)
inst2 = ILookTheSameEveryRun(a=1)
inst3 = ILookAlmostLikeBuiltinRepr(a=1)
print(inst1)
print(inst2)
print(inst3)
< /code>
запустить дважды: < /p>
ILookTheSameEveryRun
at
< /code>
ILookTheSameEveryRun
at
< /code>
I took a look at the startup flags for the python interpreter, but nothing seems to allow for this. Any workarounds? I know I could also put the repr on a Mixin and reuse that everywhere, but that's ugly too.
If I can't, that's fine and that's what I am expecting to hear. Just wondering if someone else had the same problem and found a way.
p.s. this is less about dedicated printing of instances and more about things like print(mylist)
Подробнее здесь: https://stackoverflow.com/questions/795 ... python-obj