Problems implementing a deque class with output Deque<elements>

77 views Asked by At

I'm attending a python course in university, where we should implement a deque class as an assignment. Now I've got some problems with the output an instance should give given in the docstring.

class Deque:
"""
>>> d = Deque()
>>> d
Deque<>

>>> d = d.append(1); d
Deque<1>

>>> d.append(2).prepend(0)   # allow for chaining of appending & prepending
Deque<0, 1, 2>

I have no idea how to achieve that an instance outputs this notation with the angle brackets.

Anybody got an idea?

Thanks in advance

2

There are 2 answers

1
Moses Koledoye On

You work can work that out in the __repr__ of the class.

As a simple example for an empty Deque instance:

>>> class Deque(object):
...     def __repr__(self):
...         return 'Deque<>'
...
>>> d = Deque()
>>> d
Deque<>

For a non-empty instance, you would simply format the returned string to include the contents of the instance.

0
Daniel Roseman On

You need to define the __repr__() method; you can return a string formatted however you want.