mardi 29 octobre 2019

Syntax explanation: square brackets in python

I am currently going through the book "Python 3 Object-Oriented Programming by Dusty Phillips". In the book I came across a code block that I am having a hard time understanding as I have not seen it used before. It seems to be placing square brackets [] at the end of an if else statement.

I initially thought it was referencing a list and still think this, but want to understand why the syntax is how it is. I have tried googling this problem as well as looking through stack overflow. Every example or problem that I see have items inside the brackets or are initializing a normal list.

def __init__(self, points=None):
    points = points if points else []
    self.vertices = []
    for point in points:
        if isinstance(point, tuple):
            point = Point(*point)
        self.vertices.append(point)

The line that I am not understanding in the code is line 2 where points is defined. Thank you for reading and for anyone who helps.

Aucun commentaire:

Enregistrer un commentaire