Using object.__repr__ during development

All new tutorial are submitted here.
They will be moved to the main Tutorials section once they have been approved by mods and admins.

Using object.__repr__ during development

Postby Larz60+ » Sun Feb 15, 2015 5:07 pm

Hello,

For this example, I will be using Python 3.4 on Linux (Mint 17)

Today I discovered something new (to me, not to Python). Veteran python programmers most likely already know about this, but it is so useful that I'd like to share it with those who don't.

From time to time, I experiment with Python builtins in an attempt to learn something new. This time I chose object.__repr__

The official Python 3.4 documentation reads:

------------------------
object.__str__(self)

Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.

This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.
------------------------

For example, suppose you plan on creating a class that will be used to create and manipulate key pairs (key and Value). You decide to call this class 'KeyPair'. When you are in debug mode, it would be nice for logging, or just printing to refer to these pairs by name.

This is exactly what you can do with __repr__

Example without __repr__:
Code: Select all
class KeyPair:
    def __init__(self, key=None, value=None):
        self.key = key
        self.value = value

if __name__ == '__main__':
    k1 = KeyPair('Start', [1,2,3])
    print('k1: {k}'.format(k=k1))

Results:
Code: Select all
k1: <__main__.KeyPair object at 0x7f669a917710>

Ok, this is part of, but not all of what you really want to know.

Example Now with __repr__:
Code: Select all
class KeyPair:
    def __init__(self, key=None, value=None):
        self.key = key
        self.value = value

    def __repr__(self):
        return 'KeyPair(key: %r, value: %r)' % (self.key, self.value)


if __name__ == '__main__':
    k1 = KeyPair('Start', [1,2,3])
    print('k1: {k}'.format(k=k1))

Results:
Code: Select all
k1: KeyPair(key: 'Start', value: [1, 2, 3])

Now that's what I want to see!
This is very useful to add to your class definitions.

Enjoy,

Larz60+
Last edited by Larz60+ on Sun Feb 15, 2015 11:43 pm, edited 1 time in total.
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm

Re: Using object.__repr__ during development

Postby stranac » Sun Feb 15, 2015 5:20 pm

What's up with that weirdly written string?

Code: Select all
'KeyPair(key: ''%r'', value: ''%r'')'
is the exact same thing as
Code: Select all
'KeyPair(key: %r, value: %r)'
except less readable.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Re: Using object.__repr__ during development

Postby Larz60+ » Sun Feb 15, 2015 6:15 pm

Hello,

You are correct. It's a reminant of something earlier, remove it if you wish.

Larz60+
Larz60+
 
Posts: 1307
Joined: Thu Apr 03, 2014 4:06 pm


Return to Tutorial Submissions

Who is online

Users browsing this forum: No registered users and 1 guest