What does a for loop within a list do in Python?

前端 未结 2 1251
太阳男子
太阳男子 2020-11-27 03:41

Can someone explain the last line of this Python code snippet to me?

Cell is just another class. I don\'t understand how the for loop is be

2条回答
  •  没有蜡笔的小新
    2020-11-27 04:21

    It is the same as if you did this:

    def __init__(self, region, srcPos, pos):
        self.region = region
        self.cells = []
        for i in xrange(region.cellsPerCol):
            self.cells.append(Cell(self, i))
    

    This is called a list comprehension.

提交回复
热议问题