Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-241

Python __repr__ is confusing and does not eval to the object in question

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 0.1
    • None
    • Python - Compiler
    • 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

      Attachments

        1. repr.patch
          0.8 kB
          Jonathan Ellis

        Activity

          People

            Unassigned Unassigned
            jbellis Jonathan Ellis
            Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: