python - Str implemented in class not used -


let assume have class implements str() function. when put object of class dictionary , use str() of dictionary, not use function! did wrong?

class test(object):     def __str__(self):         return("i dork") = test() print(a) x = {0:a} print(str(tuple(sorted(x.items())))) 

which returns:

i dork ((0, <__main__.test object @ 0x7fe5204ddbe0>),) 

obviously, goal have

((0, 'i dork'),) 

containers in python show representations, not string conversions. need implement object.__repr__() influence object representation.

note __str__ , __repr__ play different roles; representation there debugging purposes, __str__ string output presentation. if __str__ missing, __repr__ used, not vice versa.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -