0

My code:

class Sentence():

    def __init__(self, cells, count):
        self.cells = set(cells)
        self.count = count

class Mines():
      
   def __init__(self):
       
       self.knowledge = []

   def add_knowledge(self, cells, count):
       
        # let count=8
        # let cells=(1,1) (1,2) (1,3)..........    
       for i in range(count):
          sen=Sentence(cells, count)
          self.knowledge.append(sen)

          print(self.knowledge)

first of all I have created object of Sentence class then i am appending this in the list. then I want to print self.knowledge in loop. actually I want to print whole list (knowledge) in loop but i am not able to print the list. it gives me output like <mines.Sentence object at 0x00000000042D8748> can anybody help me how i print whole self.knowledge object.

trincot
  • 317,000
  • 35
  • 244
  • 286
sh agh
  • 21
  • 1
  • 1
    Why do you think that's wrong? The elements of the list are `Sentence` objects, and that's what `` is. – Barmar Feb 02 '22 at 16:56
  • What is the purpose of creating `Sentence` objects each time with the *same* `cells` list? What is that useful for? What do you expect as output? Can you provide the driver code that actually calls this function? – trincot Feb 02 '22 at 16:56
  • You probably want `cells[i]`. – Barmar Feb 02 '22 at 16:57
  • what is your expected output? – Ulises Bussi Feb 02 '22 at 16:57
  • 2
    If you want to change how `Sentence` objects are shown in the list, define its `__repr__` method. – Barmar Feb 02 '22 at 17:01
  • Does this answer your question? [How to apply \_\_str\_\_ function when printing a list of objects in Python](https://stackoverflow.com/questions/3558474/how-to-apply-str-function-when-printing-a-list-of-objects-in-python) – buran Feb 02 '22 at 17:03
  • What is the purpose of `count`? If it's the count of values in `cells` then don't pass the count around. Just use `len(cells)` as needed. Especially since your `self.count = count` code can persist the wrong number of cells (because you stored them in a set which will remove duplicates). – jarmod Feb 02 '22 at 17:04

1 Answers1

0

As mentioned up here, if you want to show object properties, you will have to create (overwrite) a method called __repr__ in the first object:

This is an example of an output:

class Sentence():

    def __init__(self, cells, count):
        self.cells = set(cells)
        self.count = count

    def __repr__(self):
        return f'self.cells: {self.cells} self.count: {self.count}'
    
class Mines():
      
   def __init__(self):
       
       self.knowledge = []

   def add_knowledge(self, cells, count):
       
        # let count=8
        # let cells=(1,1) (1,2) (1,3)..........    
       for i in range(count):
          sen=Sentence(cells, count)
          self.knowledge.append(sen)

       print(self.knowledge)



a=Mines()
count = 3
cells = [(1,1),(1,2),(1,3)]


a.add_knowledge(cells, count)


outputs: [self.cells: {(1, 1), (1, 2), (1, 3)} self.count: 3, self.cells: {(1, 1), (1, 2), (1, 3)} self.count: 3, self.cells: {(1, 1), (1, 2), (1, 3)} self.count: 3]

Ulises Bussi
  • 1,635
  • 1
  • 2
  • 14