6

I'm working with a class that emulates a python list. I want to return it as a python list() when I access it without an index.

with a normal list():

>>> a = [1,2,3]
>>> a
[1,2,3]

what I'm getting, essentially:

>>> a = MyList([1,2,3])
>>> a
<MyList object at 0xdeadbeef>

I can't figure out which dunder method (if any) would allow me to customize this behavior?

I'd think it would be __ get __ ? although list() doesn't implement get/set/delete - i guess because it's a built-in?

la11111
  • 361
  • 5
  • 10
  • I posted an answer, but actually I'm not sure what you mean. Can you give an example of what you want to achieve? If you want to show your list when you type `a` in the console, then you're looking for the `__repr__` method – Vincent Savard Sep 30 '12 at 22:14
  • What you are seeing in the interactive shell when you enter only the name is the repr (REPResentational) string. Same with the first one, where a real list emits a string that looks like the source code. Ideally, `eval(repr(object))` should work for simple types. So you are no "accessing" it when you show it like that, but "stringifying" it. – Keith Sep 30 '12 at 22:23
  • yes, that's exactly what I was looking for. I was confused about what was really happening there. – la11111 Sep 30 '12 at 22:24
  • @VincentSavard actually your posted answer was correct. – Ashwini Chaudhary Sep 30 '12 at 22:36

4 Answers4

5

The method you are looking for wolud be __repr__. See also http://docs.python.org/reference/datamodel.html#object.repr

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
jfrohnhofen
  • 167
  • 9
4

You should override the __repr__ method in you class (and optionally the __str__ method too), see this post for a discussion on the differences.

Something like this:

class MyList(object):
    def __repr__(self):
        # iterate over elements and add each one to resulting string

As pointed in the comments, str() calls __repr__ if __str__ isn't defined, but repr() doesn't call __str__ if __repr__ isn't defined.

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 2
    If you're only going to implement one, implement `__repr__` rather than `__str__`. `str` calls `__repr__` if `__str__` isn't defined, but `repr` doesn't call `__str__` if `__repr__` isn't defined. – senderle Sep 30 '12 at 22:38
3

A very simple example:

class MyList(object):
    def __init__(self,arg):
       self.mylist = arg
    def __repr__(self):
        return 'MyList(' + str(self.mylist) + ')'
    def __str__(self):
        return str(self.mylist)
    def __getitem__(self,i):
        return self.mylist[i]

a = MyList([1,2,3])
print a
print repr(a)
for x in a:
    print x

Output:

[1, 2, 3]
MyList([1, 2, 3])
1
2
3
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

Allow me to answer my own question - I believe it's the __ repr __ method that I'm looking for. Please correct me if i'm wrong. Here's what I came up with:

def __repr__(self):
    return str([i for i in iter(self)])
la11111
  • 361
  • 5
  • 10