726

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?

(page 2) 50 comments
sorted by: hot top controversial new old
[-] driving_crooner@lemmy.eco.br 6 points 2 weeks ago

Does everyone call the function of the script main? I never use main(), just call the function what the program is supposed to do, this program calculates the IBNR? The function is called calculate_IBNR(), then at the end of the script if name = 'main': calculate_IBNR(test_params) to test de script, then is imported into a tkinter script to be converter to an exe with pyinstaller

[-] Whelks_chance@lemmy.world 3 points 2 weeks ago

All of mine are called do_thing() because after a few days of working on it, the scope creep always means the original name was wrong anyway.

[-] iAvicenna@lemmy.world 6 points 2 weeks ago* (last edited 2 weeks ago)

wait till you see

if __name__ = "__main__":

   main()
`
[-] HiddenLayer555@lemmy.ml 4 points 2 weeks ago* (last edited 2 weeks ago)

Luckily Python is one step ahead:

Python 3.13.3 (main, Apr 22 2025, 00:00:00) [GCC 15.0.1 20250418 (Red Hat 15.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if __name__ = "__main__":
... 
...    main()
...    
    File "<python-input-0>", line 1
    if __name__ = "__main__":
        ^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Also TIL that := is a thing in Python.

load more comments (1 replies)
[-] dariusj18@lemmy.world 5 points 2 weeks ago

The if block is where my arg parser goes

[-] 10001110101@lemm.ee 3 points 2 weeks ago

I've always found needing to manually add a class instance parameter (i.e. self) to every object method really weird. And the constructors being named __init__. Not having multiple dispatch is kinda annoying too. Needing to use decorators for class methods, static methods, and abstract classes is also annoying. Now that I think about it, Python kinda sucks (even though it's the language I use the most, lol).

[-] sebsch@discuss.tchncs.de 3 points 2 weeks ago

Nah self is quite important. The main part of a method is to access the state of the object. self is just the interface to it.

[-] 10001110101@lemm.ee 4 points 2 weeks ago

Guess I just prefer languages that do it this way:

class AClass {
  var aProp = 0

  fun aMethod() {
    aProp++
  }
}

Though I suppose confusion and bugs can happen when you do something like:

class AClass {
  var aProp = 0

  fun aMethod(aProp: Int) {
    // `this.aProp` is needed to access the property
  }
}
[-] onlinepersona@programming.dev 3 points 3 weeks ago* (last edited 3 weeks ago)

Can someone explain to me how to compile a C library with "main" and a program with main? How does executing a program actually work? It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file? How does it know to execute "main"? Is it possible to have a library that can be called and also executed like a program?

Anti Commercial-AI license

[-] MajorasMaskForever@lemmy.world 5 points 3 weeks ago* (last edited 3 weeks ago)

You don't. In C everything gets referenced by a symbol during the link stage of compilation. Libraries ultimately get treated like your source code during compilation and all items land in a symbol table. Two items with the same name result in a link failure and compilation aborts. So a library and a program with main is no bueno.

When Linux loads an executable they basically look at the program's symbol table and search for "main" then start executing at that point

Windows behaves mostly the same way, as does MacOS. Most RTOS's have their own special way of doing things, bare metal you're at the mercy of your CPU vendor. The C standard specifies that "main" is the special symbol we all just happen to use

[-] anton 3 points 3 weeks ago* (last edited 2 weeks ago)

If you want to have a library that can also be a standalone executable, just put the main function in an extra file and don't compile that file when using the library as a library.
You could also use the preprocessor to do it similar to python but please don't.

Just use any build tool, and have two targets, one library and one executable:

LIB_SOURCES = tools.c, stuff.c, more.c
EXE_SOURCES = main.c, $LIB_SOURCES

Edit: added example

[-] Cratermaker@discuss.tchncs.de 2 points 3 weeks ago

I haven't done much low level stuff, but I think the 'main' function is something the compiler uses to establish an entry point for the compiled binary. The name 'main' would not exist in the compiled binary at all, but the function itself would still exist. Executable formats aren't all the same, so they'll have different ways of determining where this entry point function is expected to be. You can 'run' a binary library file by invoking a function contained therein, which is how DLL files work.

load more comments (5 replies)
[-] joyjoy@lemm.ee 2 points 3 weeks ago

Its called runpy.run_script

load more comments
view more: ‹ prev next ›
this post was submitted on 28 May 2025
726 points (100.0% liked)

Programmer Humor

24287 readers
463 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS