Also, do y’all call main() in the if block or do you just put the code you want to run in the if block?

  • chicken@lemmy.dbzer0.com
    link
    fedilink
    English
    arrow-up
    8
    ·
    4 days ago

    I usually put some print statements and stuff in the if block.

    The syntax is kind of weird and I have to regularly look it up, but it’s nice in a way because it makes it more obvious that this code isn’t going to run if you are importing the file rather than running it directly. Which implies a nice way to write something like a util.py, where there is no main function and what’s in the if block is for testing everything, not actually running the application.

  • kinther@lemmy.world
    link
    fedilink
    English
    arrow-up
    3
    ·
    4 days ago

    I write a main function that is called by the name in main if block. I understand the reason behind it and also write name in main if blocks for utilities I import (eg utilities.py), which prints usage information to direct the user on how to use things properly.

    It doesn’t make sense initially but I’ve gotten used to it. Doesn’t bother me anymore.

  • Balthazar@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    4 days ago

    I usually make the if block into the command-line entry point, which instantiates an argument parser and calls main. Maybe I should move the parsing into main so it can be tested, but…

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

    Ok, here me out, this actually makes more sense NONO!, put your pitch forks down.

    Think about it, defining the entry point like any other function is rather confusing and doesn’t distinguish it’s importance and different function in coding, the entry point is more than just where the program starts, well it is but the entry point sometimes needs to be redefined because the hardware or software requires it, for example when running C on an X86 processor you have to redefine the entry point like so:

    void _start() {
    }