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!

Комментарии • 31

  • @clorzz
    @clorzz 11 месяцев назад +7

    Might have been worth giving an example with a dot. Arithmetic in brackets is less surprising than those.

  • @noiseless2
    @noiseless2 11 месяцев назад +3

    Didn’t know about the tokenizer module . Thanks 👍

  • @m3mb3rsh1p
    @m3mb3rsh1p 9 месяцев назад

    Thank you! I have been using backslashes for long statements but this is so much better.

  • @Jakub1989YTb
    @Jakub1989YTb 11 месяцев назад +1

    And also mostly during one time of year. The advent of code :-)

  • @nibblrrr7124
    @nibblrrr7124 Месяц назад

    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.)

  • @_imps
    @_imps 11 месяцев назад +8

    that must be frustraitingly hard to write regular expression without using backslashes

    • @anthonywritescode
      @anthonywritescode  11 месяцев назад +11

      lol obviously this means syntactical backslashes and not escape sequences

  • @seasong7655
    @seasong7655 11 месяцев назад

    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 \

    • @nibblrrr7124
      @nibblrrr7124 Месяц назад

      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).

  • @berrik1584
    @berrik1584 11 месяцев назад +13

    brackets make code inside look like tuple tbh

    • @kinomonogatari
      @kinomonogatari 11 месяцев назад +19

      Agreed, but you need to remember that brackets are not part of the tuple syntax.

    • @valeriusandof9782
      @valeriusandof9782 11 месяцев назад

      ruclips.net/video/EDGJ2TMuppM/видео.htmlsi=W8h2nm4BjY7sSlQ0

    • @TheClonerx
      @TheClonerx 11 месяцев назад +1

      You mean dict? Tuples use parenthesis

    • @bzboii
      @bzboii 11 месяцев назад +8

      commas make the tuple, not the parens

  • @janAkaliKilo
    @janAkaliKilo 11 месяцев назад

    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
    """

  • @yates_
    @yates_ 11 месяцев назад

    Now here's a question you (probably) don't get often: what T-shirt is that? : v

  • @atkr1
    @atkr1 9 месяцев назад

    I watched hundreds of your videos. Great topics, content and format, keep it up!

  • @TigerWalts
    @TigerWalts 11 месяцев назад

    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.

  • @re.liable
    @re.liable 11 месяцев назад

    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

  • @datadata96
    @datadata96 11 месяцев назад

    Bill Gates open class action?

  • @realsuayip
    @realsuayip 11 месяцев назад

    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?

    • @anthonywritescode
      @anthonywritescode  11 месяцев назад +5

      there's no reason for that either -- just do one per line

    • @realsuayip
      @realsuayip 11 месяцев назад

      gotta save them bytes 😛@@anthonywritescode

  • @mad_vegan
    @mad_vegan 11 месяцев назад

    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||.

  • @aryankothari4634
    @aryankothari4634 11 месяцев назад +2

    positional only function args?

    • @anthonywritescode
      @anthonywritescode  11 месяцев назад +12

      nice try but that's a forward slash!

    • @aryankothari4634
      @aryankothari4634 11 месяцев назад +1

      @@anthonywritescode damn. forgot about that. I’ve only actually used it once in the last 5 years ahaha.

    • @ihorkovryhin1767
      @ihorkovryhin1767 11 месяцев назад

      @anthonywritescode so let me try.
      \t etc ? ;)

    • @anthonywritescode
      @anthonywritescode  11 месяцев назад +3

      those aren't syntactical backslashes (escape sequences)