List of objects in Python

Asked

Viewed 4,478 times

2

I’m doing a function that returns an object list. Snippet from the code:

def guarda_word(lista_palavras,lista_objeto,indice):

    for i in lista_palavras:
        if bool(lista_objeto) == True: # a list de objeto esta vazia
            i = PalavraIndices(i)
            i.add_item(indice,1)
            lista_objeto.append(i)
        elif i not in lista_objeto: # o elemento da lista de palavras ainda nao tem um objeto
            i = PalavraIndices(i)
            i.add_item(indice,1)
            lista_objeto.append(i)
        else:
            i.add_item(indice,1)
    return lista_objeto

lista_objeto = guarda_word(palavras_A,lista_objeto,'arquivoA')

print(lista_objeto)

The problem is, when I print this list of objects on the screen. it comes out this way:

[<__main__.PalavraIndices object at 0x005E6970>, <__main__.PalavraIndices 
object at 0x005E6990>, <__main__.PalavraIndices object at 0x005E69B0>, 
<__main__.PalavraIndices object at 0x005E69D0>, <__main__.PalavraIndices 
object at 0x005E69F0>, <__main__.PalavraIndices object at 0x005E6A30>, 
<__main__.PalavraIndices object at 0x005E6A50>, <__main__.PalavraIndices 
object at 0x005E6A70>, <__main__.PalavraIndices object at 0x005E6A90>, 
<__main__.PalavraIndices object at 0x005E6A10>, <__main__.PalavraIndices 
object at 0x005E6AB0>, <__main__.PalavraIndices object at 0x005E6AD0>, 
<__main__.PalavraIndices object at 0x005E6AF0>]

Does anyone know why the name of each object in this list is not just appearing?

2 answers

4


You must add a method __repr__(self) on the instantiated object, which will return a string as you want it to be displayed on screen.

Behold this reply for more information.

def __repr__(self):
     return str(self.__dict__)
  • show bro! worked right! thank you very much!

3

If the goal is just to show the class name, you can do so:

class Foo:
    pass
print(Foo().__class__.__name__) # Foo

DEMONSTRATION

But if you want something more personalized for each object (class instance) depending on the entries you can do for example:

class Foo:
    def __init__(self, bar):
        self.bar = bar
    def __repr__(self):
        return 'Class {} em que na sua variável de instância bar é {}'.format(self.__class__.__name__, self.bar)

print(Foo('hey')) # Class Foo em que na sua variável de instância bar é hey
print(Foo('lol')) # Class Foo em que na sua variável de instância bar é lol
print(Foo('bar')) # Class Foo em que na sua variável de instância bar é bar

DEMONSTRATION

But if you don’t declare this method, or in this case __str__(self), the output for this is just this 'general descriptive'.

There are some examples of classes that are used very often that work exactly like this:

from decimal import Decimal

print(Decimal('10.5')) # 10.5
print(Decimal('21.2')) # 21.2

DEMONSTRATION

  • 1

    I created the def __repr__(self):, in my class.. and it worked... is returning the name of each instantiated object, in the list of objects

Browser other questions tagged

You are not signed in. Login or sign up in order to post.