• pnutzh4x0r@lemmy.ndlug.org
    link
    fedilink
    English
    arrow-up
    7
    ·
    4 days ago

    Path objects also override the / operator to join paths

    This is both cool and gross… gives me C++ vibes (operator overloading abuse).

    • Midnitte@beehaw.org
      link
      fedilink
      English
      arrow-up
      3
      ·
      4 days ago

      A great joy when working with people on a combination of Windows/MacOS/Linux.

      Hell, even great when your laptop is windows and desktop (or even raspberry pi) is Linux

    • Fred@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      4 days ago

      Scapy is another library where they redefined / to layer packets, such that you can write:

      IP(dst="172.23.34.45") / UDP() / DNS(…)
      

      Then Scapy has magic so that on serialisation, the UDP layer knows defaults to dport=53 if the upper layer is DNS, and it can access the lower layer to compute its checksum.

      And don’t forget that strings have a custom % (as in modulo) operator for formatting:

      "Hello %s" %(username)
      

      Of course in modern Python, f-strings will almost always be more convenient

    • Diplomjodler@lemmy.world
      link
      fedilink
      arrow-up
      2
      arrow-down
      1
      ·
      4 days ago

      They could have chosen a better operator. But the functionality is fantastic. Makes working with paths so much easier. And you can even use slashes on windows paths.

      • leds@feddit.dk
        link
        fedilink
        arrow-up
        6
        ·
        4 days ago

        It makes the code so much more clean and readable since you’re dropping multiple levels of brackets , for example

        os.path.join( a, b, os.path.dirname(c))

        Becomes

        a / b / c.parent

        I really like it

    • unalivejoy@lemm.ee
      link
      fedilink
      English
      arrow-up
      1
      ·
      4 days ago

      Don’t forget about the helper functions mypath.read_text() and mypath.write_text(content)

    • ulterno@programming.dev
      link
      fedilink
      English
      arrow-up
      0
      arrow-down
      1
      ·
      3 days ago

      Today I realised, we all prefer semantics matching oriental language semantics.
      English: Open my path.
      Same sentence in Hindi semantics would be: My path open.