object to string in Python
This video explains
object to string in Python
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping
--
Chapters
00:00 Question
01:59 Accepted answer (Score 34)
02:26 Answer 2 (Score 3)
03:05 Thank you
--
Full question
https://stackoverflow.com/questions/4367...
Accepted answer links:
[this]: https://stackoverflow.com/a/2626364/5762...
Answer 2 links:
[image]: https://i.stack.imgur.com/s71kx.png
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #tostring
#avk47
object to string in Python
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping
--
Chapters
00:00 Question
01:59 Accepted answer (Score 34)
02:26 Answer 2 (Score 3)
03:05 Thank you
--
Full question
https://stackoverflow.com/questions/4367...
Accepted answer links:
[this]: https://stackoverflow.com/a/2626364/5762...
Answer 2 links:
[image]: https://i.stack.imgur.com/s71kx.png
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #tostring
#avk47
ACCEPTED ANSWER
Score 34
You can use __repr__ instead of __str__, which works recursively, although that is not a good idea most of the times (look at this answer for more details). Nevertheless, this works for me:
def __repr__(self):
return str(self.__dict__)
ANSWER 2
Score 3
You can try using repr instead of str.
I got the below output for print(t1), when using def repr(self): instead of str.
{'attr1': 'bean 1', 'attr2': [{'attr1': 'bean 1.1', 'attr2': 'same'}, {'attr1': 'bean 1.2', 'attr2': 42}]}
Let me know if this solves your problem. Attaching image for reference.
Regards, Vinith