Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Fixed
-
0.1
-
None
-
None
-
Patch Available
Description
Having the repr return the repr of _dict_ is confusing, because the object is not a dict and does not act (much) like one. 100% of the 3 python developers I have seen who are new to thrift were confused at first why the repr looked like a dict but obj[attributename] raised KeyError. Additionally, this violates the repr guideline that where possible eval(repr(obj)) == obj.
Finally, specifying a _str_ equal to _repr_ is redundant, since str() will use _repr_ if no _str_ is given.
Here is a patch:
$ diff -u compiler/cpp/src/generate/t_py_generator.cc.old compiler/cpp/sr\
c/generate/t_py_generator.cc
— compiler/cpp/src/generate/t_py_generator.cc.old 2008-12-24 16:36:54.000000000 +0000
+++ compiler/cpp/src/generate/t_py_generator.cc 2008-12-24 16:49:54.000000000 +0000
@@ -614,11 +614,10 @@
// Printing utilities so that on the command line thrift
// structs look pretty like dictionaries
out <<
- indent() << "def _str_(self):" << endl <<
- indent() << " return str(self._dict_)" << endl <<
- endl <<
indent() << "def _repr_(self):" << endl << - indent() << " return repr(self._dict_)" << endl <<
+ indent() << " L = ['%s=%r' % (key, value)" << endl <<
+ indent() << " for key, value in self._dict_.iteritems()]" << endl <<
+ indent() << " return '%s(%s)' % (self._class.name_, ', '.join(L))" << endl <<
endl;
// Equality and inequality methods that compare by value