Good or bad use of __repr__?

Discussion in 'Python' started by Alaric Haag, Feb 4, 2009.

  1. Alaric Haag

    Alaric Haag Guest

    Hello,

    Is the use of __repr__ below a "really bad idea"?

    class Dimension():
    def __init__(self, setp, name):
    ptr = setp.contents.dim
    while ptr.contents.name != name:
    ptr = ptr.contents.next
    self.name = ptr.contents.name
    self.size = ptr.contents.size
    self.unlimited = bool(ptr.contents.unlimited)
    self.coord = ptr.contents.coord
    def __repr__(self):
    return '%g' % (self.size)

    As written, if a program references a Dimension instance without an
    attribute, it gets the size attrbute "by default". If it wants the other
    attributes, they have to be spec'd. In the context of the code being
    developed, the "size" attribute is the "logical" representation of the
    dimension. I'm just wondering if this sort of design should be avoided.

    Many thanks!

    Alaric
    Alaric Haag, Feb 4, 2009
    #1
    1. Advertising

  2. On Tue, 03 Feb 2009 22:02:13 -0600, Alaric Haag wrote:

    > Hello,
    >
    > Is the use of __repr__ below a "really bad idea"?
    >
    > class Dimension():
    > def __init__(self, setp, name):
    > ptr = setp.contents.dim
    > while ptr.contents.name != name:
    > ptr = ptr.contents.next
    > self.name = ptr.contents.name
    > self.size = ptr.contents.size
    > self.unlimited = bool(ptr.contents.unlimited)
    > self.coord = ptr.contents.coord
    > def __repr__(self):
    > return '%g' % (self.size)



    As a rule of thumb, you should aim for:

    eval( repr(obj) )

    to recreate the obj. That's not always possible, but when possible, it is
    an ideal to aspire to. Given that, I'd recommend:

    def __repr__(self):
    return '%s(%s, %s)' % (
    self.__class__.__name__, self.ptr, self.name)
    def __str__(self):
    return "<dim=%g>" % self.size


    except of course your class doesn't store ptr.


    But looking at the code shown, I'm guessing you have bigger design
    problems than just what __repr__ should look like. I suggest you read
    this:

    http://www.surfscranton.com/architecture/LawOfDemeter.htm


    --
    Steven
    Steven D'Aprano, Feb 4, 2009
    #2
    1. Advertising

  3. On Wed, 04 Feb 2009 05:18:29 +0000, Steven D'Aprano wrote:

    > ... I'd recommend:
    >
    > def __repr__(self):
    > return '%s(%s, %s)' % (
    > self.__class__.__name__, self.ptr, self.name)
    > def __str__(self):
    > return "<dim=%g>" % self.size
    >
    >
    > except of course your class doesn't store ptr.


    Ah crap, sorry, I meant setp not ptr. Sorry for the confusion.



    --
    Steven
    Steven D'Aprano, Feb 4, 2009
    #3
  4. Alaric Haag a écrit :
    > Hello,
    >
    > Is the use of __repr__ below a "really bad idea"?


    I'd probably do the same as Stephen Hansen ("<Dimension(size=50)>") or
    at least something quite similar.


    Now on a totally unrelated point (micro optimization anyone ?):

    > class Dimension():
    > def __init__(self, setp, name):
    > ptr = setp.contents.dim
    > while ptr.contents.name != name:
    > ptr = ptr.contents.next
    > self.name = ptr.contents.name
    > self.size = ptr.contents.size
    > self.unlimited = bool(ptr.contents.unlimited)
    > self.coord = ptr.contents.coord


    In the above code, you're constantly refering to ptr.contents, and never
    use ptr directly. Attribute lookup is not free, so it's better to avoid
    them. Local bindings lookup, OTHO, is quite fast. IOW, this would better
    be written as:

    def __init__(self, setp, name):
    contents = setp.contents.dim
    while contents.name != name:
    contents = contents.next
    self.name = contents.name
    self.size = contents.size
    self.unlimited = bool(contents.unlimited)
    self.coord = contents.coord
    Bruno Desthuilliers, Feb 4, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chenyu
    Replies:
    2
    Views:
    349
    George Kinney
    Nov 6, 2003
  2. Jim Newton

    how to use __str__ and __repr__?

    Jim Newton, Jun 7, 2004, in forum: Python
    Replies:
    15
    Views:
    545
    Peter Maas
    Jun 9, 2004
  3. Dan Sommers
    Replies:
    3
    Views:
    301
    Serge Orlov
    Feb 19, 2005
  4. could ildg
    Replies:
    2
    Views:
    356
    Greg Ewing
    Apr 4, 2005
  5. rantingrick
    Replies:
    44
    Views:
    1,200
    Peter Pearson
    Jul 13, 2010
Loading...

Share This Page