Printing a list whose elements are class objects

thelma

I am trying to print a list whose elements are all objects of a user defined class, as in this simple example:

class Athing:
    def __init__(self,thething):
      self.aray = thething

  # Representation of thing for printing
  def __str__(self):          
      return '['+ ', '. join(str(i) for i in self.aray) + ']'

this_thing = []
for j in range(2):
    this_thing.append(Athing([[j,j+2,j+3], [j*2,j+5,j+6]]))
print 'this_thing =\n',this_thing
print 'this_thing[0] =\n',this_thing[0]

The code above gives me the following results:

this_thing =
[<__main__.Athing instance at 0x109dec368>, <__main__.Athing instance at 0x109dec3f8>]
this_thing[0] =
[[0, 2, 3], [0, 5, 6]]

Why can't the list of Athing objects print them without explicitly asking for the object, as in the second print, and how can I make the first print work?

sebdelsol

Printing a list containing objects uses __str__ for the list itself, but the implementation of list.__str__ calls __repr__ for each object - Athing() in your case.

So you want to override __repr__ and you won't have to override __str__ since :

If a class defines repr() but not str(), then repr() is also used when an “informal” string representation of instances of that class is required

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Printing list of class objects inside

From Dev

I get an empty list when printing multiple elements by class

From Dev

Printing an array list of objects

From Dev

Printing an list with hex elements

From Dev

Printing out elements in a list

From Dev

How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

From Dev

Printing all objects from a class

From Dev

How to convince a Java Constructor to accept a List containing objects whose Class implements a certain Interface

From Dev

Select elements whose class ends with particular string

From Dev

retrieving the elements from array whose elements are objects in javascript

From Dev

retrieving the elements from array whose elements are objects in javascript

From Dev

Adding children to a group whose elements are a string and list

From Dev

Split a list whose elements are multiple element lists

From Dev

Printing a list with elements depending on size of list

From Dev

Printing elements of a list inside list with spaces

From Dev

Printing a list from a class in Python

From Dev

Access specific child class functions whose objects are in a template class vector

From Dev

How to render an array of elements whose attributes are in a nested array of objects?

From Dev

How to render an array of elements whose attributes are in a nested array of objects?

From Dev

Printing a HashMap that contains of String and List of Objects

From Dev

Values not visible when printing list of objects

From Dev

printing list of objects as links in vanilla javascript

From Dev

Prolog list not printing all the elements on console

From Dev

Printing elements of a linked list containing void*

From Dev

Printing tree list tuple elements in F#

From Dev

Printing tree list tuple elements in F#

From Dev

store list of objects in class

From Dev

store list of objects in class

From Dev

creating a list of class objects

Related Related

  1. 1

    Printing list of class objects inside

  2. 2

    I get an empty list when printing multiple elements by class

  3. 3

    Printing an array list of objects

  4. 4

    Printing an list with hex elements

  5. 5

    Printing out elements in a list

  6. 6

    How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

  7. 7

    Printing all objects from a class

  8. 8

    How to convince a Java Constructor to accept a List containing objects whose Class implements a certain Interface

  9. 9

    Select elements whose class ends with particular string

  10. 10

    retrieving the elements from array whose elements are objects in javascript

  11. 11

    retrieving the elements from array whose elements are objects in javascript

  12. 12

    Adding children to a group whose elements are a string and list

  13. 13

    Split a list whose elements are multiple element lists

  14. 14

    Printing a list with elements depending on size of list

  15. 15

    Printing elements of a list inside list with spaces

  16. 16

    Printing a list from a class in Python

  17. 17

    Access specific child class functions whose objects are in a template class vector

  18. 18

    How to render an array of elements whose attributes are in a nested array of objects?

  19. 19

    How to render an array of elements whose attributes are in a nested array of objects?

  20. 20

    Printing a HashMap that contains of String and List of Objects

  21. 21

    Values not visible when printing list of objects

  22. 22

    printing list of objects as links in vanilla javascript

  23. 23

    Prolog list not printing all the elements on console

  24. 24

    Printing elements of a linked list containing void*

  25. 25

    Printing tree list tuple elements in F#

  26. 26

    Printing tree list tuple elements in F#

  27. 27

    store list of objects in class

  28. 28

    store list of objects in class

  29. 29

    creating a list of class objects

HotTag

Archive