0

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.

Jason R
  • 416
  • 6
  • 13
  • Try passing `None` in for `points` (or omitting it, so it uses the default `None` value) and see what happens. What value does `points` get assigned when you don't pass a value in? – ChrisGPT was on strike Oct 29 '19 at 13:01
  • 2
    `[]` is an empty list. I suspect that you are confused by the python ternary operator rather than that. `a = b if c else d` is similar to but more readable than (in e.g. Javascript) `a = c ? b : d` – John Coleman Oct 29 '19 at 13:02
  • If points is defined then go with points else initialize to an empty list. – scrappedcola Oct 29 '19 at 13:03
  • 1
    See [this question](https://stackoverflow.com/q/1132941/4996248) about *why* the code is written this way rather than just using `def __init__(self, points=[]):` Understanding that is at least as important as understanding the ternary operator. – John Coleman Oct 29 '19 at 13:22

3 Answers3

3

points = points if points else [] is a shorthand for

if points:
    points = points # points remains unchanged
else:
    points = []     # points is a new list
Cid
  • 14,968
  • 4
  • 30
  • 45
1

Think about it like this: Have you ever created an empty list?

# Like:
myList = list()
# Or:
myOtherList = []

You can (or will) see that both of them are valid ways of creating an empty list.

Now as for the points = points if points else [] line, this is called a ternary condidtional operator. The answer in the link has a great explanation how they work! The short version is, like Cid's answer says: It's an abbreviation of a full if/else statement-block.

Your specific case here basically says:

If points exists, use points. Otherwise use []

Or in other words:

If points exists, use points. Otherwise use an empty list

Kraay89
  • 919
  • 1
  • 7
  • 19
  • 1
    The addition of the "ternary conditional operator" is very helpful in understanding how they work. Since I have a good understanding of lists, just not the ternary conditional operator. Thank you for your detailed answer. – Jason R Oct 29 '19 at 15:31
0

[] simply means empty list. Line 2 says that points must be assigned as points of argument and assigned as empty list if points of argument is None.

sohnman
  • 38
  • 6