0

I'm taking a course on Polars and found the syntax I don't fully understand. Here's sample from the course:

csvFile = "..\\data\\example.csv"
(
    pl.read_csv(csvFile)
    .filter(pl.col("Col").is_null())
)

this is working. When I try to modify the line to

csvFile = "..\\data\\example.csv"
ex=(
    pl.read_csv(csvFile)
    .filter(pl.col("Col").is_null())
    
)

this is also working. But when I to make further changes to:

csvFile = "..\\data\\example.csv"
(
    ex=pl.read_csv(csvFile)
    .filter(pl.col("Col").is_null())
    
)

I get syntax error ex=pl.read_csv(csvFile) ^ SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

What is the meaning of these parentheses? I really like this kind of syntax but can't find reference on how to use it properly. I'm using python 3.11 in VSCode with Jupyter extension.

Artur

Artup
  • 49
  • 4
  • Needed for having the statement over multiple lines without escaping the newlines, see https://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python – ivvija Mar 22 '23 at 09:54
  • 1
    You can put an *expression* in parentheses, but not a *statement*. So for example `(2+2)` is valid, and so is `x=(2+2)` but `(x=2+2)` is not valid. That's what's different about the last example in your post, compared to the first. – slothrop Mar 22 '23 at 10:10
  • 1
    If you want to break your second example across lines without using a backslash, you could do `ex=(pl.read_csv(csvFile).filter(pl.col("Col").is_null()))` (note the extra parentheses that surround the part of the statement after the `=` sign). Within that, you can put a line break before any of the `.` method calls that you prefer. – slothrop Mar 22 '23 at 10:12
  • Are you familiar with the difference between an expression and a statement? `a = b` is a statement, just like `def f(): pass`, and can’t be parenthesised. Expressions like `pl.read_csv(csvFile) .filter(pl.col("Col").is_null()) ` can be found in all situations a statement can, in addition to being able to be parenthesised and can occur on the right-hand-side of an assignment operation. – dROOOze Mar 22 '23 at 10:13

2 Answers2

1

That is just a way to put on several lines what should go on a single line. It's just formatting stuff.

Both

(
    expression on 
    several lines
)

and

variable = (
    expression on
    several lines
)

works. In the first case your expression got evaluated but its output is not saved to any variable, in the second it does.

As a general rule, everything that is inside brackets (both round and squared ones) can be safely written on multiple lines for readability purposes, without needing to escape newline with a \ at the end of the lines. PEP8 contains all rules related to a good formatting of python code.

mattiatantardini
  • 525
  • 1
  • 5
  • 23
-1

A container enclosed by parenthesis is named a tuple. Tuples contain non-mutable order of items inside.

Tuple is similar to a List, where a Tuple is enclosed by () and a List is enclosed by [].

For example:

tuple_1 = (item1, item2, item3)
list_1 = [item1, item2, item3]
  • 4
    This is completely wrong. There is no trailing comma, so the expression simply evaluates to whatever `pl.read_csv(...).filter(...)` returns, not a tuple. – InSync Mar 22 '23 at 10:07