0

i have a class for lists that creates list objects and deals with list actions, and i have another class 'Dog' to create Dog objects. However, i can´t manage to print the atributes of class 'Dog'(name and age)after appending an object of type 'Dog' to a list of type 'Dogslist'. Instead by printing the list it prints the addresses of the objects. How can i print name and age of all the dogs i have in the list?

class Dogslist():

  def __init__(self):
    self.lista = []

  def append(self, object):
    self.lista.append(object)

  def delete(self):
    pass

  def showlist(self):
    pass


class Dog:

  def __init__(self, name, age):
    self.__name = name
    self.__age = age

  def description(self):
    return self.__name, self.__age


a=Dog("Geralt", 10)
b=Dog("Vesemir", 13)

doglist = Dogslist()

doglist.append(a)
doglist.append(b)
print(doglist.lista)

The output shows the following wich refers to objects' addresses:

[<__main__.Dog object at 0x7f25ed30c0a0>, <__main__.Dog object
 at 0x7f25ed2f18b0>]
  • This is the default string representation of any object, inherited from `object.__repr__`, you must define your own `__repr__` if you want it to pint anything else. – juanpa.arrivillaga Apr 16 '20 at 03:39
  • As an aside, don't use double-underscore name-mangling if you dont *want* double-underscore name mangling. Why did you use it? – juanpa.arrivillaga Apr 16 '20 at 03:40
  • @juanpa.arrivillaga I only used double-underscore because i wanted to use private variables, for encapsulation. I read somewhere that writting double-underscore before the variable name makes that variable private. Otherwise i never use double-underscore – sergio_baixo Apr 16 '20 at 09:46

3 Answers3

2

Implement __str__ or __repr__ for Dog so Python knows how to represent it.

class Dog:
    def __init__(self, name, age):
        self.__name = name
        self.__age = age
    def __repr__(self):
        return f"Dog({self.__name}, {self.__age})"
    def __str__(self):
        return f"Woof! I'm {self.__name}, and I'm {self.__age} years old!"

More information about __str__ and __repr__ can be found here

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • It worked, but only with __repr__implemented. I tried defining __str__also but still returns the addresses. Thanks a lot! – sergio_baixo Apr 16 '20 at 09:48
  • i want to return the two attributes of Dog(name and age) into two different variables, making both a list with names and a list with ages of all dogs.__repr__ doesn't let me return two variables. How do i manage to do that? – sergio_baixo Apr 16 '20 at 13:44
  • Just use normal list constructions for that. `dog_names = [d.__name for d in dog_list]; dog_ages = [d.__age for d in dog_list]` – Adam Smith Apr 16 '20 at 20:34
0

You should be able to access the objects like below( because those attributes were defined as 'private' and you cant access them directly) .

for item in doglist.lista:
    print(item.__dict__['_Dog__name'])
    print(item.__dict__['_Dog__age'])

Simpler to just not make those attributes private.Then you would be able to access it like item.name

Emerson
  • 1,136
  • 1
  • 6
  • 9
-1
class Dogslist():

  def __init__(self):
    self.lista = []

  def append(self, objectt):
    self.lista.append(objectt)

  def delete(self):
    pass

  def showlist(self):
    return(self.lista)

Dogslist.lista technically only exists within the class so you can't call it from outside. You need a function in that class that returns Dogslist.lista

class Dog:

  def __init__(self, name, age):
    self.__name = name
    self.__age = age

  def description(self):
    return self.__name, self.__age


a=Dog("Geralt", 10)
b=Dog("Vesemir", 13)

doglist = Dogslist()

Same with the dogs.

doglist.append(a.description())
doglist.append(b.description())
print(doglist.showlist())

Result:

[('Geralt', 10), ('Vesemir', 13)]
Cheng An Wong
  • 308
  • 2
  • 8
  • "Dogslist.lista technically only exists within the class so you can't call it from outside." No, this is totally wrong, you don't need a function at all. – juanpa.arrivillaga Apr 16 '20 at 03:39