简体   繁体   中英

How do I iterate through a list of objects

I'm trying to print a list(phonebook) of objects(record) but I'm new to python and it is not recognizing that record is a objects in the list. How would I call objects in this instance?

Ive tried looking at tutorials of python for loops but none reference how to call an object in a list.

class record:
    def __init__(self,telephone,lastname,firstname):
        self.telephone = telephone
        self.lastname = lastname
        self.firstname = firstname

class PhoneBook:
    def __init__(self):
        self.phonebook = []

    def printphonebook(self):
        for record in self.phonebook:
            x = 0
            print(self.phonebook[x])
            x = x + 1

Expected output would be the list of objects including the telephone number, last name, and first name.

You want to print an instance of a class. So you should provide the special __str__ method to tell python how the object should be printed. __str__() must return a string: here the docs.

class record:
    def __init__(self,telephone,lastname,firstname):
        self.telephone = telephone
        self.lastname = lastname
        self.firstname = firstname

    def __str__(self):
        #returning a string with the content, you can edit the string to fit your needs.
        #here I am using formatted string literals, works with python >= 3.6
        return f"Last name: {self.lastname}, First Name: {self.firstname}, Telephone: {self.telephone}"

class PhoneBook:
    def __init__(self):
        self.phonebook = []

    def printphonebook(self):
        for entry in self.phonebook:
            print(entry)

What happens here is that when you call print(record) the __str__() method is used to provide a string representing the content of the instance.

So if you do:

book = PhoneBook()
book.phonebook.append(record(800, "Wayne", "Bruce"))
book.phonebook.append(record(1234,  "Kent", "Clark"))
book.phonebook.append(record(499, "Prince", "Diana"))

book.printphonebook()

This will print:

Last name: Wayne, First Name: Bruce, Telephone: 800
Last name: Kent, First Name: Clark, Telephone: 1234
Last name: Prince, First Name: Diana, Telephone: 499

  1. You have no elements in your self.phonebook . Of course it prints nothing.
  2. Every iteration you create x=0 so you always will print the first item:
class record:
    def __init__(self,telephone,lastname,firstname):
        self.telephone = telephone
        self.lastname = lastname
        self.firstname = firstname

class PhoneBook:
    def __init__(self):
        self.phonebook = [1,2,3,4,5]

    def printphonebook(self):
        for record in self.phonebook:
            x = 0
            print(self.phonebook[x])
            x = x + 1

a = PhoneBook()
a.printphonebook()
1
1
1
1
1
  1. Your x index is really pointless, you can just print record :
class record:
    def __init__(self,telephone,lastname,firstname):
        self.telephone = telephone
        self.lastname = lastname
        self.firstname = firstname

class PhoneBook:
    def __init__(self):
        self.phonebook = [1,2,3,4,5]

    def printphonebook(self):
        for record in self.phonebook:
            print(record)

a = PhoneBook()
a.printphonebook()
1
2
3
4
5

So: 1. Fill your self.phonebook with ANY elements 2. Print record , without indices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM