I don't use backslashes (with one exception) (beginner - intermediate) anthony explains
HTML-код
- Опубликовано: 2 окт 2024
- today I talk about the backslash character in python and why I avoid it -- but also the one case where I do use it :)
playlist: • anthony explains
==========
twitch: / anthonywritescode
dicsord: / discord
twitter: / codewithanthony
github: github.com/aso...
stream github: github.com/ant...
I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below!
Might have been worth giving an example with a dot. Arithmetic in brackets is less surprising than those.
Didn’t know about the tokenizer module . Thanks 👍
Thank you! I have been using backslashes for long statements but this is so much better.
And also mostly during one time of year. The advent of code :-)
One other place I often use \ before linebrek is assert statements:
assert quite_long_boolean_expression \
f"Even longer explicit error message with {helpful_information}"
For quick sanity-checks, "if not expression: raise ..." feels like more clutter, and I'd feel compelled to go the whole nine yards and do Proper Error Handling (sensible Exception type, structured info instead of just in the string ...).
(Also, no one uses -O, don't @ me.)
yeah my point is that looks super gross
that must be frustraitingly hard to write regular expression without using backslashes
lol obviously this means syntactical backslashes and not escape sequences
I always use them in my path strings. Like when you copy a path from windows like "C:
ew folder" you often have to add more \
Usually, I go for raw strings instead*: r"C:\windows\path"
Just beware that a backslash at the end of a raw string still has to be escaped: r"C:\why\tho\\"
Or use pathlib.Path:
Path("C:/forward/slashes/as/God/intended") / "append relative paths/with the slash operator.txt"
* Raw string are also useful for regular expressions, which rely heavily on backslash escapes. For long, complicated regexes, use the re.VERBOSE flag so you can have raw triple-quoted strings over multiple lines, maybe even with (?# comments).
brackets make code inside look like tuple tbh
Agreed, but you need to remember that brackets are not part of the tuple syntax.
ruclips.net/video/EDGJ2TMuppM/видео.htmlsi=W8h2nm4BjY7sSlQ0
You mean dict? Tuples use parenthesis
commas make the tuple, not the parens
I like how Nim (language which also uses significant whitespace) solves both of these two problems:
1. Nim allowes indentation for readabilty after operators, an open parenthesis and after commas
2. Nim will ignore newline that's following the triple quote
this is syntactically valid Nim:
let a =
5
let b =
[1, 2, 3, 4].map((x) => x+8)
.sum()
let text = """
first line
second line
"""
Now here's a question you (probably) don't get often: what T-shirt is that? : v
I watched hundreds of your videos. Great topics, content and format, keep it up!
I agree, as useful as it is, \NEWLINE is ugly and not needed.
That said, I'm also happy with starting a multiline right after the triple-quotes. I think I have only used """\ once.
caught this from your strings video :) Personally been using `.strip()` a lot, usually as `.strip().splitlines()` to get a list of strings without manually quoting them
I also really like the "grouping" approach, so much so that I'd even force Black to use it by adding a comment inside the group, e.g.
```py
x = (
#
3 * 2
+ 4
)
```
Makes commenting out parts much easier
Bill Gates open class action?
Was recently reading ruff formatter and they have decided to use backslashes to wrap very long global/nonlocal variable declarations (i.e., each line gets one variable). I guess thats a valid case as well?
there's no reason for that either -- just do one per line
gotta save them bytes 😛@@anthonywritescode
I actually like using backslashes, especially for arithmetic expressions when the line barely fits. It's clean and compact syntax, and more aligned with how we write math equations.
This just reminded me that in Matlab there's a backslash operator: X = A\B. It's like division, but for rectangular matrices. It gives the result which minimizes the error ||BX - A||.
positional only function args?
nice try but that's a forward slash!
@@anthonywritescode damn. forgot about that. I’ve only actually used it once in the last 5 years ahaha.
@anthonywritescode so let me try.
\t etc ? ;)
those aren't syntactical backslashes (escape sequences)