Class using the iter protocol

MarkS

I have a small class that creates a building. I am wanting to learn more about iter. The following works:

class Building(object):
    def __init__(self, floors):
        self._floors = [None] * floors

    def __setitem__(self, floor_number, data):
        self._floors[floor_number] = data

    def __getitem__(self, floor_number):
        return self._floors[floor_number]

    def __iter__(self):
        for floor_number in self._floors:
            yield self._floors[floor_number]


building1 = Building(9)  # Construct a building with 9 floors
building1[0] = 'Reception'
building1[1] = 'ABC Corp'
building1[2] = 'DEF Inc'
building1[3] = 'Apple'
building1[4] = 'Cisco'
building1[5] = 'Microsoft'
building1[6] = 'Dell'
building1[7] = 'EMC'
building1[8] = 'HPE'

'''
for floor in building1:
    print(building1[floor])
'''

print(building1[6])

Dell

But if I un-comment out the for loop I get this:

Traceback (most recent call last):
  File "C:/Users/Mark/PycharmProjects/main/main.py", line 28, in <module>
    for floor in building1:
  File "C:/Users/Mark/PycharmProjects/main/main.py", line 13, in __iter__
    yield self._floors[floor_number]
TypeError: list indices must be integers or slices, not str

I understand that list indices have to be integers. What I am not getting is why this for loop is violating that restriction.

What I am wanting to do is exercise the iter protocol and print out every floor in the building, not just a specific one.

Martijn Pieters

You misunderstood what a for loop does; you don't have indices, you have your actual values in the list. Just yield those:

for contents in self._floors:
    yield contents

You need to do the same in your outer loop:

for contents in building1:
    print(contents)

Python for loops are Foreach loops, you iterate over the values, not an index.

Instead of looping, your __iter__ method could just return an iterator for the list, effectively delegating to the list iterator that iter() produces for you:

def __iter__(self):
    return iter(self._floors)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related