0

I have a class that wraps around python deque from collections. When I go and create a deque x=deque(), I create a empty deque object. So if I fill it up: x.append(0) and simply type in x on the console, i get:

In[78]: x
Out[78]: deque([0])

My question is how can I output the same thing as above when I have a wrapper for class deque. For example.

class deque_wrapper:
    def __init__(self):
        self.data_structure = deque()

    def newCustomAddon(x):
        return len(self.data_structure)

Ie

In[74]: x = deque_wrapper()
In[75]: x
Out[75]: <__main__.deque_wrapperat 0x7e3d0f0>

I want to customize what gets printed out as oppose to just a memory location. What can I do?

1
  • One other issue tehre: in Python 2.x, you have to inherit from "object" - just inheriting from "nothing" will fail you in subtle and hard to debug ways. Commented Sep 8, 2014 at 1:56

2 Answers 2

3

I want to customize what gets printed out as oppose to just a memory location. What can I do?

This is exactly what __repr__ is for:

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.

Because you didn't define a __repr__, you're getting the default implementation from object (assuming Python 3… otherwise, you've written a classic class, which is a bad idea, and you don't want to learn how they get their defaults when you can just stop using them…), which just returns that string with the object's type name and address.

Note the __str__ method below __repr__ in the docs. If the most human-readable representation and the valid-Python-expression representation are not the same, define both methods. Otherwise, just define __repr__, and __str__ will use it by default.


So, if you want to print the exact same thing as deque, just delegate __repr__:

def __repr__(self):
    return repr(self.data_structure)

If you want to wrap it in something:

def __repr__(self):
    return '{}({!r})'.format(type(self).__name__, self.data_structure)

Note that I didn't call repr in the second version, because that's exactly what !r means in a format string. But really, in this case, you don't need either; a deque has the same str and repr.

Sign up to request clarification or add additional context in comments.

1 Comment

deque always has building in array like referencing. From my above example, how can I let my custom function use x[0]? Cause right now I can print it out all good, but I can using x[n-th element]
3

Delegate the generation of the representation.

class deque_wrapper:
   ...
  def __repr__(self):
    return repr(self.data_structure)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.