Working with graphs - Kevin Bacon game

Chris Angelico rosuav at gmail.com
Wed Jan 9 05:00:12 EST 2019


On Wed, Jan 9, 2019 at 8:56 PM <jskako at gmail.com> wrote:
> class cvor:
>     __slots__ = ('ime','susjed')
>
> My problem is that when I print graph with "print (graph)" I am getting:
>
> "[<__main__.cvor object at 0x000001475275EBE0>, <__main__.cvor object at 0x000001475275EEF0>, <__main__.cvor object at 0x000001475275EFD0>, <__main__.cvor object at 0x000001475275EE80>, <__main__.cvor object at 0x000001475275EB70>, <__main__.cvor object at 0x000001475275ED68>,..."
>

When you print out a collection of arbitrary objects, Python shows you
the *repr* ("representation") of each one. The default repr for a
custom class just shows the class name and the object's unique ID,
which isn't terribly useful. Create your own custom representation by
adding a method to your cvor class:

def __repr__(self):
    return "some nice descriptive string"

It's up to you to decide how to build that string, but that's what
will be shown.

Incidentally, you may want to consider the namedtuple type; it might
be more what you want.

ChrisA



More information about the Python-list mailing list