Mai N.
Mai N.

Reputation: 33

What does this line of code related to __str__(self) mean?

Help me understand what object means in this line: s = ' ' + object.__str__(obj). I have not seen object referred to anywhere in the code, is it a special keyword? What does it mean in this context?

The link to the full code: http://greenteapress.com/thinkpython2/code/GoodKangaroo.py

I can't wrap my head around that one line

def __str__(self):
        """Return a string representaion of this Kangaroo.
        """
        t = [ self.name + ' has pouch contents:' ]
        for obj in self.pouch_contents:
            s = '    ' + object.__str__(obj)
            t.append(s)
        return '\n'.join(t)

Upvotes: 1

Views: 514

Answers (3)

Marco Bonelli
Marco Bonelli

Reputation: 69276

That line of code is a way of converting obj into a string, by calling the default __str__ method of the base type object, which generates a string containing the class name and the address of the instance in memory, like for example <Kangaroo instance at 0xAABBCC>.

Normally, one would use str(obj), but in this case, if obj is another Kangaroo, then the same __str__() method defined in Kangaroo would be called recursively, thus causing something like this to be generated:

foo = Kangaroo('foo')
bar = Kangaroo('bar')
baz = Kangaroo('baz')

baz.put_in_pouch(1)
bar.put_in_pouch(baz)
foo.put_in_pouch(bar)

# Result of print str(foo)

foo has pouch contents:
    bar has pouch contents:
    baz has pouch contents:
    1

Using object.__str__() instead, avoids calling the method recursively and gives:

# Result of print str(foo)

foo has pouch contents:
    <__main__.Kangaroo instance at 0x7fc3a864d128>

Upvotes: 3

David Culbreth
David Culbreth

Reputation: 2776

object refers to the builtin base class, which is an object. typing object into the Python REPL provides this...

>>> object
<class 'object'>

It is the base class included with the standard scope in python.


Here, this is the only reference I can actually find in the docs.

object

Any data with state (attributes or value) and defined behavior (methods). Also the ultimate base class of any new-style class.


I FOUND IT!

class object Return a new featureless object. object is a base for all classes. It has the methods that are common to all instances of Python classes. This function does not accept any arguments.

Upvotes: 1

Alex
Alex

Reputation: 1101

object is python's base class.

In this case the code is calling object.__str__(self) which will use object's method of turning obj into a string. This will call object.__repr__(self) which will print out the "official" representation of the object.

For strings this will be '[string contents]' (with [string contents] replaced with the actual contents of the string) and for generic objects this will be <[object name] at [address]> (again with [object name] and [address] replaced with the object's actual name and address).

Note: object.__str__(obj) and str(obj) will return different results since object.__str__(obj) will eventually call repr(obj).

Upvotes: 0

Related Questions