So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers.
So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script.
The main two reasons that I can think of to include this even when you have no intention of importing this as a library are:
For unit testing you will need to import as a module.
Sometimes I will run a python interactive interpreter and then import my script so that I can do some manual testing without needing to change my main function or if stmt.
Oh that is a good point actually. It's been a while since I have done any serious Python, so I'm not sure why you couldn't just use convention instead of this conditional.
For my part, if a Python script is meant to be executed, then I'll give it a shebang, drop the .py, and simply mark it as executable in the filesystem. 🤷‍♂️
This is exactly why the conditional is used. It allows the script to function both as a standalone application and a library.
ETA: Probably would make sense to just treat it as default behavior in the interpreter and only require the conditional to overwrite in cases where main is not the main function and/or pre-processing is needed.
Yeah, and also zero dependency management, so you are free to figure out what combination of Python, Tensorflow and Keras will make it not throw random exceptions.
And don't forget the number one rule: you must use all the graphing libraries, all the time.
python isn't the only language to do "execute everything imported from a particular file and all top level statements get run". both node and c# (but with restrictions on where top level statements can be) can do that type of thing, I'm sure there's more.
python conventions are unique because they attempt to make their entrypoint also importable itself without side effects. almost no one needs to do that, and I imagine the convention leaked out from the few people that did since it doesn't hurt either.
for instance in node this is the equivalent, even though I've never seen someone try before:
if (path.resolve(url.fileURLToPath(import.meta.url)).includes(path.resolve(process.argv[1])))
{
// main things
}
I definitely do for quick scripts, but I try to break this habit. The biggest advantage of def main() is that variables are local and not accessible to other functions defined in the same script, which can sometimes help catch bugs or typos.
If the file is just a class I usually put example usage with some default arguments in that block by itself. There is no reason for a "main" function. It's a nice obvious block that doesn't run when someone imports the class but if they're looking at the class there is a really obvious place to see the class usage. No confusion about what "main()" is meant to do.
if __name__ == '__main__':
# MyClass example Usage
my_object = MyClass()
my_object.my_method()
It simply swaps some things around to make things more confusing, then goes into an infinite loop (whether or not you import or execute it standalone). it's no different than just including in the global scope:
while True:
pass
I was kinda lazy with the fuckery, tbh. I could have gotten much more confusing, but don't have too much time today. :-)
Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language
I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.
To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.
Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.
Well now. My primary exposure to Go would be using it to take first place in my company's 'Advent of Code' several years ago, in order to see what it was like, after which I've been pleased never to have to use it again. Some of our teams have used it to provide microservices - REST APIs that do database queries, some lightweight logic, and conversion to and from JSON - and my experience of working with that is that they've inexplicably managed to scatter all the logic among dozens of files, for what might be done with 80 lines of Python. I suspect the problem in that case is the developers, though.
It has some good aspects - I like how easy it is to do a static build that can be deployed in a container.
The actual language itself I find fairly abominable. The lack of exceptions means that error handling is all through everything, and not necessarily any better than other modern languages. The lack of overloads means that you'll have multiple definitions of eg. Math.min cluttering things up. I don't think the container classes are particularly good. The implementation of pointers seems solely implemented to let you have null pointer exceptions, it's a pointless wart.
If what you're wanting to code is the kind of thing that Google do, in the exact same way that Google do it, and you have a team of hipsters who all know how it works, then it may be a fine choice. Otherwise I would probably recommend using something else.
This is the most excellent summary of Go I have ever read. I agree with everything you've said, although as a fan of Scala and in particular its asynchronous programming ecosystem (cats for me, but I'll forgive those who prefer the walled garden of zio) I would also add that, whilst its async model with go routines is generally pretty easy to use, it can shit the bed on some highly-concurrent workloads and fail to schedule stuff in time that it really should've, and because it's such a mother-knows-best language there's fuck all you can do to give higher priority to the threads that you happen to know need more TLC
I'm now 1 year in to working in Go having been mostly C++ and then mostly large-scale Python dev (with full type annotation).
Frankly, I bristle now at people giving Python a hard time, having worked with Go and I now hate Go and the de-facto ethos that surrounds it. Python may be slow, but for a lot of use cases not in any way that matters and modern computers are very fast. Many problem areas are not performance-limited, and many performance problems are algorithmic, not from raw statement execution. I even rewrote an entire system in Python and made it use 20% of the CPU the former C++ solution used, while having much more functionality.
The error returns drive me nuts. I looked around for explanations of the reasoning as I wasn't seeing it, and only found bald assertions that exceptions get out of control and somehow error returns don't. Meanwhile standard Go code is very awkward to read because almost every little trivial function calls becomes 4 lines of code, often to do nothing but propagate the error (and errors are just ignored if you forget...). With heavy use of context managers, my error and cancellation handling in Python was always clean, clear, and simple, with code that almost read like whiteboard pseudo-code.
The select statement can be cool in Go, but then you realize that literally 98% of the times it's used, it's simply boilerplate code to (verbosely) handle cancellation semantics via the context object you have to pass everywhere. Again, literally code you just don't need in exception-based languages with good structures to manage it like Python context managers.
And every time you think "this is stupidly awkward and verbose, surely there's a cleaner way to do this" you find people online advocating writing the same boilerplate code and passing it off as a virtue. e.g. get a value from a map and fall back to a default if it's not there? Nope, not offering that, so everyone must write their own if foo, ok := m[k]; !ok {...} crap. Over and over and over again the answer is "just copy this chunk of code" rather than "standard libraries should provide these commonly needed utilities". Of course we can do anything we want ourselves, it's Turing Complete, but why would we want to perpetually reinvent these wheels?
It's an unpopular language, becoming less popular (at least by Google trends) and for good reason. I can see it working well for a narrow set of low level activities with extreme concurrency performance needs, but it's not the only language that could handle that, and for everything else, I think it's the wrong choice.
I used it for a while and I think it's been one of the best languages I've tried. C for example is too barebones for modern desktop apps. Apps written in Rust are great but most of the time, it's just not worth the effort. And stuff like Python, JS is... uhh.. where do I even begin
I think Go hits the sweet spot between these. Unlike C, it at least has some simple error/panic mechanism, GC so you don't have to worry about memory much and some modern features on top of that. And unlike Python it can actually create reasonably snappy programs.
In any programming language, there will always be multiple cases where you need to link C libraries. CGo, although people don't seem to be adoring it, is actually... okay? I mean of course it does still have some overhead but it's still one of the nicer ways to link C libraries with your code. And Go being similar to C makes writing bindings so much easier
Multithreading in Go is lovely. Or as I read somewhere "you merely adopted multithreading, I was born with it"
Packaging is handled pretty nicely, pulling a library from the net is fairly trivial. And the standard directory structure for Go, although I'm not used to it, makes organizing stuff much easier and is easy to adopt
As you would've guessed from the amount of times I mentioned C in this comment, I basically see Go as the "bigger C for different situations"
I'm gonna have to disagree here, it's always a guessing game of how many layers of abstraction they've used to seemingly avoid writing any implementation code... Can't put the code related to "bicycles" in the Bicycle class, no, that obviously goes in WheeledDeviceServiceFactoryBeanImpl that's in the 'utils' package.
Enough of that crazy talk - plainly WheeledDeviceServiceFactoryBeanImpl is where the dependency injection annotations are placed. If you can decide what the code does without stepping through it with a debugger, and any backtrace doesn't have at least two hundred lines of Spring boot, then plainly it isn't enterprise enough.
Fair enough, though. You can write stupid overly-abstract shit in any language, but Java does encourage it.
Does Lua rank far below python for you? I have so much rage against it. At least with Python I don't have to do a bunch of steps to just get it to do something. May take me a while to get through bad Python code, but Lua makes my eyes bleed and I begin to regret my life choices in trying to understand wtf these people wrote.
One thing I really dislike about Python is the double underscore thing, just really looks ugly to me and feels excessive. Just give me my flow control characters that aren't whitespace
I'm at peace with balanced underscores (like "dunder name equals dunder main") and the internal ones for snake case, but in the unbalanced ones (prefixing unders and dunders for pseudo-private) still bug me. But at least, conventionally, it's visually the same idea as Hungarian notation.
The if block is still in the global scope, so writing the code in it is a great way to find yourself scratching your head with a weird bug 30 minutes later.
I know right? It even has var with implicit typing now. While I prefer Kotlin any day, there's been quite a few qol improvements to Java over the last few years.
What's hacky about an introspective language providing environment to all of the executing code, so that the coder can make the decision about what to do?
It would by hacky if Python decided "We'll arbitrarily take functions named "main" and execute them for you, even though we already started execution at the top of the file."
For C, this is less so. The body of the file isn't being executed, it's being read and compiled. Without a function to act as a starting point, it doesn't get executed.
Reserving main is definitely more hacky. Try compiling multiple objects with main defined into a single binary - it won't go well. This can make a lot of testing libraries rather convoluted, since some want to write their own main while others want you to write it because require all kinds of macros or whatever.
On the other hand, if __name__ == "__main__" very gracefully supports having multiple entrypoints in a single module as well as derivative libraries.
I don't understand. What do you mean by deciding what the code should do in the context of language design? Can you give a concrete example? I am confused because the "main" function is required when you make an executable. Otherwise, a library will not contain any main function and we could compile it just fine no? (Shared library)
Python is an interpreted language that doesn't need a main function explicitly. You can define any package entry points you want at the package config level. (setup.py, etc)
example:
What I meant was I prefer language that treat developers like adults. If I want ptrhon's "ux" to hide some functions or objects I can do that with underscores, but nothing is private, a developer using my library can do whatever they want with it, access whatever internals they want (at their own risk of course)
Is it? I really don't think so. What can you propose that's better? I think if __name__ == __main__ works perfectly fine and can't really think of anything that would be better.
And you don't have to use it either if you don't want to anyway, so no, I don't think it's that much of a hack. Especially when the comic compares C as an example, which makes no sense to me whatsoever.
It really doesn't. It's a scripting language, functions are there but at it's core it runs a script. The issue is that it was so easy to start with that people started doing everything in it, even though it sucks for anything past complex scripts
I didn't say it wasn't real, it's just a scripting structure and not object oriented, so it doesn't make sense for it to start by looking for a "main" object
Procedural and OOP aren't mutually exclusive terms. Most OOP programs are ultimately procedural in nature. Often, the only difference is that the first argument to the function is to the left the function name and separated by a dot.
fair, I just think it's misleading to call python procedural, but it lines up with what the commenter above was describing and searching for the term for
I'd say the term "procedural" itself is an issue. Pretty much any language can be done that way if you choose. IIRC, the creator of Clojure wanted Java to work more that way, and he did it by having a single class full of functions. It's not a natural way to write Java, and that's why he invented Clojure.
That is not how Python works. There are very few languages that work by executing line-by-line anymore. Unix shell scripts are one of the few holdouts. JavaScript also does it to a certain extent; the browser starts executing line-by-line while a compiler step works in the background. Once the compiler is done, it starts execution of the compiled form right where the line-by-line execution left off. It helps JavaScript be more responsive since it doesn't have to wait for the compiler to finish.
I don't know if this applies to other shells, but bash will not only execute your script line-by-line, it will also read it line-by-line. Which means that you can modify the behavior of a running script by editing lines that have not yet been executed*. It's absolutely bonkers, and I'm sure that it has caused more than one system failure, during upgrades.
* For example, if you run the following script
echo "hello"
sleep 5
echo "goodbye"
and then edit the third line before the 5 second sleep has elapsed, then the modified line will be executed.
compared with other languages at the time, the ease of access and readability makes it worth it. plus, the heavy duty stuff is usually handled by more optimised code line numpy or sklearn...
Readability? Me eyes bleed from a day of partially staring at python code, and there is a whole another week of that ahead. Tzinch (Edit: Tzeentch) help me
Like in every programming language, it depends who wrote the code. OK, *nearly every programming language, see: LISP.
You can write cryptic, write-only programs in about any language, but you can even write readable and maintainable PERL scripts (despite people claiming this to be impossible).
As much as I am inclined to agree with this, still can't
see: LISP
Also, see: Python with more than three lines of logic. I could suspect that's just the me-versus-whitespaces thing, but no, YAML files do not get me dizzy in under thirty seconds of reading. Van Rossum made a huge miscalculation here
Everyone's welcome to their opinion of course, but I find Python more readable than anything else and I resent the visual clutter required to make intentions plain in other languages. Feels like having a conversation where people say the words "comma", "period", etc.
I also spend more time with Python than anything else and I suspect these two facts about me relate, lol
Someone should get their hands on someone like me and someone like you and study their brains. I spend most time with PHP and C++, and Python looks like an attempt to write code like prose literature. Very interesting how much of this is habbit, as it can't be just that: reading prose and poetry in English/Russian/Japanese never produced this kind of resentment
I would love that! I do think there are probably interesting underlying personality factors / preferences for a lot of this stuff as well.
I do think that many of Python's characteristics map to my own personality and I bet there's something to that. Things like syntax of course, but not strictly syntax, also things like "The Zen of Python", and the way its a "jack-of-all-trades, master-of-none". I also really kind of need the freedom and accompanying responsibility to break any "rules" on a whim (Python will happily let you overwrite its own internals while running, for instance), but I almost never do anything that uses it...
I could probably keep going lol. Feels like a "people looking like their pets" scenario, lmao
Python has a bunch of magic variables, like __name__. This one contains the name of the module you're currently in (usually based on the file name), so if your file is called foo.py, it will have the value foo.
But that's only if your module is being imported by another module. If it's executed directly (e.g. python foo.py), it will instead have a __name__ of __main__. This is often used to add a standalone CLI section to modules - e.g. the module usually only defines functions that can be imported, but when executed it runs an example of those functions.
Basically, when you compile a program written in Rust or C/C++ (the first and second panels respectively), the compiler needs to know what's supposed to be executed first when the program is run directly (i.e. when you click on the executable), which in these languages, is denoted by a special function called main(). Executable files can also contain functions and data structures that can be called by other programs, and when they are, you wouldn't want to run an entire complex and resource intensive program if another program only needs to call a single function from it. In that case, the other program will call the function it wants but not main, so only that function executes and not the entire program.
However, Python is a scripting language that's interpreted. So every Python source file is executable provided you have the Python runtime. Python also doesn't have native support for main functions in the same way Rust and C/C++ does, and it will execute every line of code as it reads the source file. This is why a single line Python file that just calls print is valid, it doesn't need to be wrapped in a main function to execute. However, what if your Python file is both meant to be executed directly and provides functions that other Python files can call? If you just put the main routine in the root of the file, it would be executed every time another program tries to import the file in order to call functions from it, since the import causes the file to be interpreted and executed in its entirety. You can still just have a main function in your file, but since Python doesn't natively support it, your main function won't do anything if you run the file directly because as far as Python is concerned, there is no executable code at the root of the file and you haven't called any functions.
The workaround is to have a single if statement at the root of the file that looks like this:
if __name__ == '__main__':
main()
It checks a special variable called __name__. If the Python file is directly executed, __name__ will have the value of the string '__main__', which satisfies the if statement so main() is called. If another Python file imports it, the value of __name__ will be the name of that file, so main() isn't called. It's clunky and not that efficient, but, 1, it works, and 2, if you cared about efficiency, you wouldn't be writing it in Python.
For Python and some other languages, this is the start of the file.
For other languages, this is a special function name reserved for this purpose - generally, "main".
In the first kind of language, the thought process is basically: I have the flow of execution, starting at the top of the file. If I want to make a library, I should build the things I want to build, then get out of the way.
In the other kind of language, the thought process is basically: I am building a library. If I want to make an executable, I should create an entry point they the execution starts at.
Python doesn't need the name main check to function at all. that's just a convenience feature that lets developers also include arbitrary entry points into modules that are part of a library and expected to be used as such. If you're writing a script, a file with a single line in it reading print("hello world") will work fine when run: python thescript.py
In the first kind of language, the thought process is basically: I have the flow of execution, starting at the top of the file. If I want to make a library, I should build the things I want to build, then get out of the way.
Note the "I have the flow of execution", and the "if I want to build a library".
If you just want to build an executable, do as you wish, you already have the flow of execution.
If you want to build a library, make the relevant classes and functions and get out of the way (i.e., no IO, no long-running tasks).
If you want to combine them, use the main name check - or, make a package and do entry points that way. Either way works, because both can fulfill the goal of staying out of the way of those importing this as a library.
I intended this an sarcastic example; I think it's worse than putting the main outside of the branch because of the extra indent-level. It does have an upside that the main() doesn't exist if you try import this as an module.
I use if__name__main__ often when working with AWS Lambda, but I also want to run it locally. Lambda wants to call a function with the params event and context. So I would do something like this:
def handler(event, context):
things
return {
'statusCode': 200,
'body': 'Hello from Lambda!'
}
if __name__ == '__main__':
event = {}
context = {}
response = handler(event, context)
print(response)
Yeah. I like using main() that way too. It's usually just a high-level function that handles globals relevant to running in standalone and calling other functions to do work.
if debug.getinfo(1).what == "main" then
-- ...
end
Not that you'll ever use it. No, seriously.
Edit: actually, they are not quite equivalent. This code just checks whether we are outside any function, not necessarily in the main file (i.e. not in a module). I don't think there's an equivalent to Python's __name__ in stock Lua.
The point of the name==main logic is that it checks if that is the file that was invoked (like running python filename.py). If you just put a main() in the global scope it will be called either when the file is invoked or loaded (which can cause unintended consequences).
It is "built-in" as the name is part of python. However, Python runs top to bottom, rather than having a special entrypoint. So name is just a utility you can use in your design.
While it can be a good practice to define a main entrypoint, that's more of a design decision and not hard rule. Many applications would not benefit from it because there is only one way to actually call the application to begin with.
Edit: Also not a dumb question. All programming languages have unique elements to them due to how they were envisioned and or developed over time (Pythons 30 years old)
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
Alternative: put entry point code in file __main__.py & run the containing package (eg, some_package) as a top-level expression (eg, python -m some_package).
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 '='?
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).
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?
It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file?
Depends on OS. Linux will look at the first bytes of the file, either see (ASCII) #! (called a shebang) or ELF magic, then call the appropriate interpreter with the executable as an argument. When executing e.g. python, it's going to call /usr/bin/env with parameters python and the file name because the shebang was #!/usr/bin/env python.
How does it know to execute “main”?
Compiled C programs are ELF so it will go through the ELF header, figure out which ld.so to use, then start that so that it will find all the libraries, resolve all dynamic symbols, then do some bookkeeping, and jump to _start. That is, it doesn't: main is a C thing.
Is it possible to have a library that can be called and also executed like a program?
Absolutely. ld.so is an example of that.. Actually, wait, I'm not so sure any more, I'm getting things mixed up with libdl.so. In any case ld.so is an executable with a file extension that makes it look like a library.
EDIT: It does work. My (GNU) libc spits out version info when executed as an executable.
If you want to start looking at the innards like that I would suggest starting here: Hello world in assembly. Note the absence of a main function, the symbol the kernel actually invokes is _start, the setup necessary to call a C main is done by libc.so. Don't try to understand GNU's libc it's full of hystarical raisins I would suggest musl.
EDIT: It does work. My (GNU) libc spits out version info when executed as an executable.
How does that work? There must be something above ld.so, maybe the OS? Because looking at the ELF header, ld.so is a shared library "Type: DYN (Shared object file)"
$ readelf -hl ld.so
ELF Header:
Magic: 7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - GNU
ABI Version: 0
Type: DYN (Shared object file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x1d780
Start of program headers: 64 (bytes into file)
Start of section headers: 256264 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 11
Size of section headers: 64 (bytes)
Number of section headers: 23
Section header string table index: 22
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000db8 0x0000000000000db8 R 0x1000
LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000
0x0000000000029435 0x0000000000029435 R E 0x1000
LOAD 0x000000000002b000 0x000000000002b000 0x000000000002b000
0x000000000000a8c0 0x000000000000a8c0 R 0x1000
LOAD 0x00000000000362e0 0x00000000000362e0 0x00000000000362e0
0x0000000000002e24 0x0000000000003000 RW 0x1000
DYNAMIC 0x0000000000037e80 0x0000000000037e80 0x0000000000037e80
0x0000000000000180 0x0000000000000180 RW 0x8
NOTE 0x00000000000002a8 0x00000000000002a8 0x00000000000002a8
0x0000000000000040 0x0000000000000040 R 0x8
NOTE 0x00000000000002e8 0x00000000000002e8 0x00000000000002e8
0x0000000000000024 0x0000000000000024 R 0x4
GNU_PROPERTY 0x00000000000002a8 0x00000000000002a8 0x00000000000002a8
0x0000000000000040 0x0000000000000040 R 0x8
GNU_EH_FRAME 0x0000000000031718 0x0000000000031718 0x0000000000031718
0x00000000000009b4 0x00000000000009b4 R 0x4
GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 0x10
GNU_RELRO 0x00000000000362e0 0x00000000000362e0 0x00000000000362e0
0x0000000000001d20 0x0000000000001d20 R 0x1
The program headers don't have interpreter information either. Compare that to ls "Type: EXEC (Executable file)".
$ readelf -hl ls
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x40b6e0
Start of program headers: 64 (bytes into file)
Start of section headers: 1473672 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 14
Size of section headers: 64 (bytes)
Number of section headers: 32
Section header string table index: 31
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
0x0000000000000310 0x0000000000000310 R 0x8
INTERP 0x00000000000003b4 0x00000000004003b4 0x00000000004003b4
0x0000000000000053 0x0000000000000053 R 0x1
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x0000000000007570 0x0000000000007570 R 0x1000
LOAD 0x0000000000008000 0x0000000000408000 0x0000000000408000
0x00000000000decb1 0x00000000000decb1 R E 0x1000
LOAD 0x00000000000e7000 0x00000000004e7000 0x00000000004e7000
0x00000000000553a0 0x00000000000553a0 R 0x1000
LOAD 0x000000000013c9c8 0x000000000053d9c8 0x000000000053d9c8
0x000000000000d01c 0x0000000000024748 RW 0x1000
DYNAMIC 0x0000000000148080 0x0000000000549080 0x0000000000549080
0x0000000000000250 0x0000000000000250 RW 0x8
NOTE 0x0000000000000350 0x0000000000400350 0x0000000000400350
0x0000000000000040 0x0000000000000040 R 0x8
NOTE 0x0000000000000390 0x0000000000400390 0x0000000000400390
0x0000000000000024 0x0000000000000024 R 0x4
NOTE 0x000000000013c380 0x000000000053c380 0x000000000053c380
0x0000000000000020 0x0000000000000020 R 0x4
GNU_PROPERTY 0x0000000000000350 0x0000000000400350 0x0000000000400350
0x0000000000000040 0x0000000000000040 R 0x8
GNU_EH_FRAME 0x0000000000126318 0x0000000000526318 0x0000000000526318
0x0000000000002eb4 0x0000000000002eb4 R 0x4
GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 0x10
GNU_RELRO 0x000000000013c9c8 0x000000000053d9c8 0x000000000053d9c8
0x000000000000c638 0x000000000000c638 R 0x1
It feels like somewhere in the flow there is the same thing that's happening in python just more hidden. Python seems to expose it because a file can be a library and an executable at the same time.
EDIT: ...with which I meant, modulo brainfart: My libc.so.6 contains a proper entry address, while other libraries are pointing at 0x0 and coredump when executed. libc.so is a linker script, presumably because GNU compulsively overcomplicates everything.
...I guess that's enough for the kernel. It might be a linux-only thing, maybe even unintended and well linux doesn't break userspace.
Speaking of, I was playing it a bit fast and loose: _start is merely the default symbol name for the entry label, I'm sure nasm and/or ld have ways to set it to something different.
Btw, ld.so is a symlink to ld-linux-x86-64.so.2 at least on my system. It is an statically linked executable. The ld.so is, in simpler words, an interpreter for the ELF format and you can run it:
ld.so --help
Entry point address: 0x1d780
Which seems to be contained in the only executable section segment of ld.so
LOAD 0x0000000000001000 0x0000000000001000 0x0000000000001000
0x0000000000028bb5 0x0000000000028bb5 R E 0x1000
Edit: My understanding of this quite shallow; the above is a segment that in this case contains the entirety of the .text section.
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
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:
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.
There are a lot of other helpful replies in this thread, so I won't add much, but I did find this reference, which you could read if you have a lot of free time. But I particularly liked reading this summary:
_start calls the libc __libc_start_main;
__libc_start_main calls the executable __libc_csu_init (statically-linked part of the libc);
__libc_csu_init calls the executable constructors (and other initialisatios);
160 Comments
arschflugkoerper@feddit.org · 86 pts · 1y
What kind of psychopath would put the code in the if block.
HiddenLayer555@lemmy.ml · 72 pts · 1y
Looks at all the Python scripts in my bin folder that I wrote.
30p87@feddit.org · 64 pts · 1y
Never heard of
?
grrgyle@slrpnk.net · 48 pts · 1y
I remember how weird this looked the first time I saw it and while I may now understand it, it still looks jank af
frezik@midwest.social · 9 pts · 1y
Python: I'm so readable that I'm practically executable pseudo-code
Also Python:
if __name__ == '__main__':. . .bane_killgrind@slrpnk.net · 7 pts · 1y
Now think about this, you have logic that doesn't make sense when run directly, but you need it to be a library.
You have multiple name=main statements in some of your functions
grrgyle@slrpnk.net · 3 pts · 1y
I'm not sure I'm following the implication. Name=main is for scripts primary, is it not?
I've never thought to add more than one of these conditionals anyway...
bane_killgrind@slrpnk.net · 2 pts · 1y
So you might have a script that does stuff as a library, and it should get environment variables and other info from the calling script. You use the same script for doing one off stuff on different computers.
So you make it do something slightly different or make it set it's path and look into the current folder when you run it directly. This change in logic could be in a few points in the script.
Anomalocaris@lemm.ee · 3 pts · 1y
I still wonder why.
unless it's for something that you want to work as an importable module and a standalone tool, then why do you need that?
Archr@lemmy.world · 3 pts · 1y
The main two reasons that I can think of to include this even when you have no intention of importing this as a library are:
grrgyle@slrpnk.net · 1 pts · 1y
Oh that is a good point actually. It's been a while since I have done any serious Python, so I'm not sure why you couldn't just use convention instead of this conditional.
For my part, if a Python script is meant to be executed, then I'll give it a shebang, drop the .py, and simply mark it as executable in the filesystem. 🤷‍♂️
nickwitha_k@lemmy.sdf.org · 1 pts · 1y
This is exactly why the conditional is used. It allows the script to function both as a standalone application and a library.
ETA: Probably would make sense to just treat it as default behavior in the interpreter and only require the conditional to overwrite in cases where
mainis not the main function and/or pre-processing is needed.HiddenLayer555@lemmy.ml · 24 pts · 1y
Heard of it, was too lazy to do it that way.
To be fair I now do it that way, but not when I was learning Python.
firelizzard@programming.dev · 4 pts · 1y
What is the point of this?
30p87@feddit.org · 7 pts · 1y
Not having tons of code in one if statement, but in a function.
IronKrill@lemmy.ca · 12 pts · 1y
And scope. Variables declared in the if can be read everywhere, variables declared in the function are limited to that function.
firelizzard@programming.dev · 2 pts · 1y
I thought you were saying to literally use
def main(): pass, that’s why I was confused30p87@feddit.org · 1 pts · 1y
Oh, no, that's just the usual placeholder. Though, ... would also be valid iirc, and would fit better as a "TODO" placeholder
Anomalocaris@lemm.ee · 11 pts · 1y
custom bin folders are a realm no God dares to tread
SubArcticTundra@lemmy.ml · 2 pts · 1y
I can and I do
*trollface*
eager_eagle@lemmy.world · 32 pts · 1y
I work in an academic / research environment. Depending who wrote it, even seeing a
__name__ == "__main__"is a bit of a rare thing...SpaceNoodle@lemmy.world · 23 pts · 1y
Academic code is absolutely horrific.
Fortunately, it is possible to translate it for practical applications.
arbitrary_sarcasm@lemmy.world · 13 pts · 1y
As someone in academia who writes code, I can confirm.
HK65@sopuli.xyz · 15 pts · 1y
Do you also have nothing but love for those 50+ cell Jupyter notebooks that don't use a single function and have everything in the global scope?
eager_eagle@lemmy.world · 14 pts · 1y
the best thing is when not even the author knows the correct order of running the cells; because of course it isn't top-to-bottom.
HK65@sopuli.xyz · 11 pts · 1y
Yeah, and also zero dependency management, so you are free to figure out what combination of Python, Tensorflow and Keras will make it not throw random exceptions.
And don't forget the number one rule: you must use all the graphing libraries, all the time.
brian@programming.dev · 4 pts · 1y
python isn't the only language to do "execute everything imported from a particular file and all top level statements get run". both node and c# (but with restrictions on where top level statements can be) can do that type of thing, I'm sure there's more.
python conventions are unique because they attempt to make their entrypoint also importable itself without side effects. almost no one needs to do that, and I imagine the convention leaked out from the few people that did since it doesn't hurt either.
for instance in node this is the equivalent, even though I've never seen someone try before:
wise_pancake@lemmy.ca · 15 pts · 1y
Why would you waste a function call on something so completely redundant?
For real though, arg parsing goes in the if, then gets dispatched to whatever function call is needed to run the proper script.NeatNit@discuss.tchncs.de · 10 pts · 1y
I definitely do for quick scripts, but I try to break this habit. The biggest advantage of
def main()is that variables are local and not accessible to other functions defined in the same script, which can sometimes help catch bugs or typos.LeninOnAPrayer@lemm.ee · 8 pts · 1y
If the file is just a class I usually put example usage with some default arguments in that block by itself. There is no reason for a "main" function. It's a nice obvious block that doesn't run when someone imports the class but if they're looking at the class there is a really obvious place to see the class usage. No confusion about what "main()" is meant to do.
Anomalocaris@lemm.ee · 1 pts · 1y
you can, no one stopping you
slacktoid@lemmy.ml · 49 pts · 1y
I call main() in the if
bastion@feddit.nl · 7 pts · 1y
I always use
..and earlier in the code:
This helps to prevent people from arbitrarily running my code as a library or executable when I don't went them to.
owsei@programming.dev · 15 pts · 1y
slacktoid@lemmy.ml · 4 pts · 1y
Can you elaborate on this blood magic?
bastion@feddit.nl · 5 pts · 1y
It simply swaps some things around to make things more confusing, then goes into an infinite loop (whether or not you import or execute it standalone). it's no different than just including in the global scope:
I was kinda lazy with the fuckery, tbh. I could have gotten much more confusing, but don't have too much time today. :-)
slacktoid@lemmy.ml · 1 pts · 1y
Lol OK I was wondering how would this run
And yes you should!!
embed_me@programming.dev · 44 pts · 1y
Sometimes I have the misfortune of working with python code written by someone else and I wonder how a language like this became anything more than a scripting language
addie@feddit.uk · 37 pts · 1y
I feel that Python is a bit of a 'Microsoft Word' of languages. Your own scripts are obviously completely fine, using a sensible and pragmatic selection of the language features in a robust fashion, but everyone else's are absurd collections of hacks that fall to pieces at the first modification.
To an extent, 'other people's C++ / Bash scripts' have the same problem. I'm usually okay with 'other people's Java', which to me is one of the big selling points of the language - the slight wordiness and lack of 'really stupid shit' makes collaboration easier.
Now, a Python script that's more than about two pages long? That makes me question its utility. The 'duck typing' everywhere makes any code that you can't 'keep in your head' very difficult to reason about.
pmk@lemmy.sdf.org · 4 pts · 1y
How do you feel about other peoples Go code?
addie@feddit.uk · 6 pts · 1y
Well now. My primary exposure to Go would be using it to take first place in my company's 'Advent of Code' several years ago, in order to see what it was like, after which I've been pleased never to have to use it again. Some of our teams have used it to provide microservices - REST APIs that do database queries, some lightweight logic, and conversion to and from JSON - and my experience of working with that is that they've inexplicably managed to scatter all the logic among dozens of files, for what might be done with 80 lines of Python. I suspect the problem in that case is the developers, though.
It has some good aspects - I like how easy it is to do a static build that can be deployed in a container.
The actual language itself I find fairly abominable. The lack of exceptions means that error handling is all through everything, and not necessarily any better than other modern languages. The lack of overloads means that you'll have multiple definitions of eg.
Math.mincluttering things up. I don't think the container classes are particularly good. The implementation of pointers seems solely implemented to let you have null pointer exceptions, it's a pointless wart.If what you're wanting to code is the kind of thing that Google do, in the exact same way that Google do it, and you have a team of hipsters who all know how it works, then it may be a fine choice. Otherwise I would probably recommend using something else.
silasmariner@programming.dev · 2 pts · 1y
This is the most excellent summary of Go I have ever read. I agree with everything you've said, although as a fan of Scala and in particular its asynchronous programming ecosystem (cats for me, but I'll forgive those who prefer the walled garden of zio) I would also add that, whilst its async model with go routines is generally pretty easy to use, it can shit the bed on some highly-concurrent workloads and fail to schedule stuff in time that it really should've, and because it's such a mother-knows-best language there's fuck all you can do to give higher priority to the threads that you happen to know need more TLC
sping@lemmy.sdf.org · 2 pts · 1y
I'm now 1 year in to working in Go having been mostly C++ and then mostly large-scale Python dev (with full type annotation).
Frankly, I bristle now at people giving Python a hard time, having worked with Go and I now hate Go and the de-facto ethos that surrounds it. Python may be slow, but for a lot of use cases not in any way that matters and modern computers are very fast. Many problem areas are not performance-limited, and many performance problems are algorithmic, not from raw statement execution. I even rewrote an entire system in Python and made it use 20% of the CPU the former C++ solution used, while having much more functionality.
The error returns drive me nuts. I looked around for explanations of the reasoning as I wasn't seeing it, and only found bald assertions that exceptions get out of control and somehow error returns don't. Meanwhile standard Go code is very awkward to read because almost every little trivial function calls becomes 4 lines of code, often to do nothing but propagate the error (and errors are just ignored if you forget...). With heavy use of context managers, my error and cancellation handling in Python was always clean, clear, and simple, with code that almost read like whiteboard pseudo-code.
The
selectstatement can be cool in Go, but then you realize that literally 98% of the times it's used, it's simply boilerplate code to (verbosely) handle cancellation semantics via the context object you have to pass everywhere. Again, literally code you just don't need in exception-based languages with good structures to manage it like Python context managers.And every time you think "this is stupidly awkward and verbose, surely there's a cleaner way to do this" you find people online advocating writing the same boilerplate code and passing it off as a virtue. e.g. get a value from a map and fall back to a default if it's not there? Nope, not offering that, so everyone must write their own
if foo, ok := m[k]; !ok {...}crap. Over and over and over again the answer is "just copy this chunk of code" rather than "standard libraries should provide these commonly needed utilities". Of course we can do anything we want ourselves, it's Turing Complete, but why would we want to perpetually reinvent these wheels?It's an unpopular language, becoming less popular (at least by Google trends) and for good reason. I can see it working well for a narrow set of low level activities with extreme concurrency performance needs, but it's not the only language that could handle that, and for everything else, I think it's the wrong choice.
yogurtwrong@lemmy.world · 3 pts · 1y
I used it for a while and I think it's been one of the best languages I've tried. C for example is too barebones for modern desktop apps. Apps written in Rust are great but most of the time, it's just not worth the effort. And stuff like Python, JS is... uhh.. where do I even begin
I think Go hits the sweet spot between these. Unlike C, it at least has some simple error/panic mechanism, GC so you don't have to worry about memory much and some modern features on top of that. And unlike Python it can actually create reasonably snappy programs.
In any programming language, there will always be multiple cases where you need to link C libraries. CGo, although people don't seem to be adoring it, is actually... okay? I mean of course it does still have some overhead but it's still one of the nicer ways to link C libraries with your code. And Go being similar to C makes writing bindings so much easier
Multithreading in Go is lovely. Or as I read somewhere "you merely adopted multithreading, I was born with it"
Packaging is handled pretty nicely, pulling a library from the net is fairly trivial. And the standard directory structure for Go, although I'm not used to it, makes organizing stuff much easier and is easy to adopt
As you would've guessed from the amount of times I mentioned C in this comment, I basically see Go as the "bigger C for different situations"
sping@lemmy.sdf.org · 2 pts · 1y
Go code is always an abomination.
addie@feddit.uk · 1 pts · 1y
Succinctly and well put.
ebc@lemmy.ca · 2 pts · 1y
I'm gonna have to disagree here, it's always a guessing game of how many layers of abstraction they've used to seemingly avoid writing any implementation code... Can't put the code related to "bicycles" in the
Bicycleclass, no, that obviously goes inWheeledDeviceServiceFactoryBeanImplthat's in the 'utils' package.addie@feddit.uk · 2 pts · 1y
Enough of that crazy talk - plainly
WheeledDeviceServiceFactoryBeanImplis where the dependency injection annotations are placed. If you can decide what the code does without stepping through it with a debugger, and any backtrace doesn't have at least two hundred lines of Spring boot, then plainly it isn't enterprise enough.Fair enough, though. You can write stupid overly-abstract shit in any language, but Java does encourage it.
driving_crooner@lemmy.eco.br · 1 pts · 1y
How many lines are in a page?
SaharaMaleikuhm@feddit.org · 7 pts · 1y
Sounds like a skill issue on your end
embed_me@programming.dev · 7 pts · 1y
Agreed. I program mainly in C so its easier for me to make sense of bad C code than bad python code which just makes me cry
jadedwench@lemmy.dbzer0.com · 1 pts · 1y
Does Lua rank far below python for you? I have so much rage against it. At least with Python I don't have to do a bunch of steps to just get it to do something. May take me a while to get through bad Python code, but Lua makes my eyes bleed and I begin to regret my life choices in trying to understand wtf these people wrote.
embed_me@programming.dev · 1 pts · 1y
Never had the opportunity to work with it. Python is much more pervasive compared to Lua I reckon
laurelraven@lemmy.zip · 35 pts · 1y
One thing I really dislike about Python is the double underscore thing, just really looks ugly to me and feels excessive. Just give me my flow control characters that aren't whitespace
weissbinder@feddit.org · 7 pts · 1y
Php says hello
atx_aquarian@lemmy.world · 3 pts · 1y
I'm at peace with balanced underscores (like "dunder name equals dunder main") and the internal ones for snake case, but in the unbalanced ones (prefixing unders and dunders for pseudo-private) still bug me. But at least, conventionally, it's visually the same idea as Hungarian notation.
eager_eagle@lemmy.world · 35 pts · 1y
The
ifblock is still in the global scope, so writing the code in it is a great way to find yourself scratching your head with a weird bug 30 minutes later.benni@lemmy.world · 1 pts · 1y
Interesting observation. Can you give an example where this is relevant?
d_k_bo@feddit.org · 32 pts · 1y
Still better than having to create a new class just to implement
Relevant Fireship video: https://youtu.be/m4-HM_sCvtQ
Damarus@feddit.org · 18 pts · 1y
Since Java 21, this has been shortened significantly. https://www.baeldung.com/java-21-unnamed-class-instance-main
frezik@midwest.social · 15 pts · 1y
Only took 27 years to make the Java "Hello, world!" kinda sane.
d_k_bo@feddit.org · 6 pts · 1y
Free standing functions in Java?! This can't be true.
Damarus@feddit.org · 10 pts · 1y
I know right? It even has var with implicit typing now. While I prefer Kotlin any day, there's been quite a few qol improvements to Java over the last few years.
mexicancartel@lemmy.dbzer0.com · 1 pts · 1y
Impossible.
_____@lemm.ee · 25 pts · 1y
Python people explaining fail to see the point: Yes we know dunders exist. We just want you to say: "Yeah, that is a bit hacky, isn't it?"
drmoose@lemmy.world · 17 pts · 1y
Tbh reserving "main" is just a hacky if not more so than checking
__name__if you actually understand language design.bastion@feddit.nl · 10 pts · 1y
Yeah, this is it.
What's hacky about an introspective language providing environment to all of the executing code, so that the coder can make the decision about what to do?
It would by hacky if Python decided "We'll arbitrarily take functions named "main" and execute them for you, even though we already started execution at the top of the file."
For C, this is less so. The body of the file isn't being executed, it's being read and compiled. Without a function to act as a starting point, it doesn't get executed.
namingthingsiseasy@programming.dev · 8 pts · 1y
Reserving
mainis definitely more hacky. Try compiling multiple objects withmaindefined into a single binary - it won't go well. This can make a lot of testing libraries rather convoluted, since some want to write their ownmainwhile others want you to write it because require all kinds of macros or whatever.On the other hand,
if __name__ == "__main__"very gracefully supports having multiple entrypoints in a single module as well as derivative libraries.bitfucker@programming.dev · 2 pts · 1y
What is not hacky then in a language design?
_stranger_@lemmy.world · 2 pts · 1y
Letting the developer decide what the code should do.
bitfucker@programming.dev · 1 pts · 1y
I don't understand. What do you mean by deciding what the code should do in the context of language design? Can you give a concrete example? I am confused because the "main" function is required when you make an executable. Otherwise, a library will not contain any main function and we could compile it just fine no? (Shared library)
_stranger_@lemmy.world · 2 pts · 1y
Python is an interpreted language that doesn't need a main function explicitly. You can define any package entry points you want at the package config level. (setup.py, etc)
example: What I meant was I prefer language that treat developers like adults. If I want ptrhon's "ux" to hide some functions or objects I can do that with underscores, but nothing is private, a developer using my library can do whatever they want with it, access whatever internals they want (at their own risk of course)
TheNamlessGuy@lemmy.world · 1 pts · 1y
drmoose@lemmy.world · 1 pts · 1y
Most contemporary python tools like flask or uvicorn do exactly this and require an explicit entry point
namingthingsiseasy@programming.dev · 3 pts · 1y
Is it? I really don't think so. What can you propose that's better? I think
if __name__ == __main__works perfectly fine and can't really think of anything that would be better.And you don't have to use it either if you don't want to anyway, so no, I don't think it's that much of a hack. Especially when the comic compares C as an example, which makes no sense to me whatsoever.
Anomalocaris@lemm.ee · 2 pts · 1y
aren't most of not all conventions hacky anyways?
MTK@lemmy.world · 22 pts · 1y
It really doesn't. It's a scripting language, functions are there but at it's core it runs a script. The issue is that it was so easy to start with that people started doing everything in it, even though it sucks for anything past complex scripts
It is the excel of databases.
frezik@midwest.social · 13 pts · 1y
What's the difference between a "scripting" language and a "real" one?
jenesaisquoi@feddit.org · 3 pts · 1y
A scripting language controls an existing binary. A non-scripting language is used to create a new binary.
JackbyDev@programming.dev · 2 pts · 1y
Scripting languages are real. Generally people consider dynamic languages scripting languages but it's not that simple.
MTK@lemmy.world · -8 pts · 1y
I didn't say it wasn't real, it's just a scripting structure and not object oriented, so it doesn't make sense for it to start by looking for a "main" object
mmddmm@lemm.ee · 11 pts · 1y
I don't think we have a name for what you are trying to say here.
(And yeah, "object oriented" isn't it.)
AllHailTheSheep@sh.itjust.works · 1 pts · 1y
procedural programming is more akin to that, but python has far to many oop concepts to be considered procedural imo
frezik@midwest.social · 4 pts · 1y
Procedural and OOP aren't mutually exclusive terms. Most OOP programs are ultimately procedural in nature. Often, the only difference is that the first argument to the function is to the left the function name and separated by a dot.
AllHailTheSheep@sh.itjust.works · 2 pts · 1y
fair, I just think it's misleading to call python procedural, but it lines up with what the commenter above was describing and searching for the term for
frezik@midwest.social · 2 pts · 1y
I'd say the term "procedural" itself is an issue. Pretty much any language can be done that way if you choose. IIRC, the creator of Clojure wanted Java to work more that way, and he did it by having a single class full of functions. It's not a natural way to write Java, and that's why he invented Clojure.
frezik@midwest.social · 3 pts · 1y
What would make it "object oriented"?
MTK@lemmy.world · 8 pts · 1y
You know what, I take that back: https://docs.python.org/3.13/faq/general.html#what-is-python
mmddmm@lemm.ee · -8 pts · 1y
It's a scripting language. What means that the computer runs it line by line, without needing to get the entire project first.
frezik@midwest.social · 14 pts · 1y
That is not how Python works. There are very few languages that work by executing line-by-line anymore. Unix shell scripts are one of the few holdouts. JavaScript also does it to a certain extent; the browser starts executing line-by-line while a compiler step works in the background. Once the compiler is done, it starts execution of the compiled form right where the line-by-line execution left off. It helps JavaScript be more responsive since it doesn't have to wait for the compiler to finish.
fruitcantfly@programming.dev · 2 pts · 1y
I don't know if this applies to other shells, but bash will not only execute your script line-by-line, it will also read it line-by-line. Which means that you can modify the behavior of a running script by editing lines that have not yet been executed*. It's absolutely bonkers, and I'm sure that it has caused more than one system failure, during upgrades.
* For example, if you run the following script
and then edit the third line before the 5 second sleep has elapsed, then the modified line will be executed.
JackbyDev@programming.dev · 1 pts · 1y
I have run into the problem of modifying a bash script while it is running.
mmddmm@lemm.ee · -5 pts · 1y
Python still has the
-ioption, and it still runs the same language as the files interface.frezik@midwest.social · 3 pts · 1y
The
-ioption is simply interactive mode. All commands still go through a compiler.the_crotch@sh.itjust.works · 8 pts · 1y
Excel recently added the ability to run python code lol
Anomalocaris@lemm.ee · 5 pts · 1y
compared with other languages at the time, the ease of access and readability makes it worth it. plus, the heavy duty stuff is usually handled by more optimised code line numpy or sklearn...
Shanmugha@lemmy.world · 1 pts · 1y
Readability? Me eyes bleed from a day of partially staring at python code, and there is a whole another week of that ahead. Tzinch (Edit: Tzeentch) help me
Treczoks@lemmy.world · 3 pts · 1y
Like in every programming language, it depends who wrote the code. OK, *nearly every programming language, see: LISP.
You can write cryptic, write-only programs in about any language, but you can even write readable and maintainable PERL scripts (despite people claiming this to be impossible).
Shanmugha@lemmy.world · -2 pts · 1y
As much as I am inclined to agree with this, still can't
Also, see: Python with more than three lines of logic. I could suspect that's just the me-versus-whitespaces thing, but no, YAML files do not get me dizzy in under thirty seconds of reading. Van Rossum made a huge miscalculation here
PolarKraken@programming.dev · 4 pts · 1y
Everyone's welcome to their opinion of course, but I find Python more readable than anything else and I resent the visual clutter required to make intentions plain in other languages. Feels like having a conversation where people say the words "comma", "period", etc.
I also spend more time with Python than anything else and I suspect these two facts about me relate, lol
Shanmugha@lemmy.world · 1 pts · 1y
Someone should get their hands on someone like me and someone like you and study their brains. I spend most time with PHP and C++, and Python looks like an attempt to write code like prose literature. Very interesting how much of this is habbit, as it can't be just that: reading prose and poetry in English/Russian/Japanese never produced this kind of resentment
PolarKraken@programming.dev · 1 pts · 1y
I would love that! I do think there are probably interesting underlying personality factors / preferences for a lot of this stuff as well.
I do think that many of Python's characteristics map to my own personality and I bet there's something to that. Things like syntax of course, but not strictly syntax, also things like "The Zen of Python", and the way its a "jack-of-all-trades, master-of-none". I also really kind of need the freedom and accompanying responsibility to break any "rules" on a whim (Python will happily let you overwrite its own internals while running, for instance), but I almost never do anything that uses it...
I could probably keep going lol. Feels like a "people looking like their pets" scenario, lmao
sheridan@lemmy.world · 19 pts · 1y
Could someone explain this please? I'm still a noob.
FooBarrington@lemmy.world · 100 pts · 1y
Python has a bunch of magic variables, like
__name__. This one contains the name of the module you're currently in (usually based on the file name), so if your file is calledfoo.py, it will have the valuefoo.But that's only if your module is being imported by another module. If it's executed directly (e.g.
python foo.py), it will instead have a__name__of__main__. This is often used to add a standalone CLI section to modules - e.g. the module usually only defines functions that can be imported, but when executed it runs an example of those functions.DragonTypeWyvern@midwest.social · 71 pts · 1y
checks username
So it's you they're always talking about
kamenlady@lemmy.world · 6 pts · 1y
It is, it's the other Barry.
d00ery@lemmy.world · 3 pts · 1y
Really helpful explanation, thanks.
HiddenLayer555@lemmy.ml · 39 pts · 1y
Basically, when you compile a program written in Rust or C/C++ (the first and second panels respectively), the compiler needs to know what's supposed to be executed first when the program is run directly (i.e. when you click on the executable), which in these languages, is denoted by a special function called
main(). Executable files can also contain functions and data structures that can be called by other programs, and when they are, you wouldn't want to run an entire complex and resource intensive program if another program only needs to call a single function from it. In that case, the other program will call the function it wants but not main, so only that function executes and not the entire program.However, Python is a scripting language that's interpreted. So every Python source file is executable provided you have the Python runtime. Python also doesn't have native support for main functions in the same way Rust and C/C++ does, and it will execute every line of code as it reads the source file. This is why a single line Python file that just calls print is valid, it doesn't need to be wrapped in a main function to execute. However, what if your Python file is both meant to be executed directly and provides functions that other Python files can call? If you just put the main routine in the root of the file, it would be executed every time another program tries to import the file in order to call functions from it, since the import causes the file to be interpreted and executed in its entirety. You can still just have a main function in your file, but since Python doesn't natively support it, your main function won't do anything if you run the file directly because as far as Python is concerned, there is no executable code at the root of the file and you haven't called any functions.
The workaround is to have a single if statement at the root of the file that looks like this:
It checks a special variable called
__name__. If the Python file is directly executed,__name__will have the value of the string'__main__', which satisfies the if statement so main() is called. If another Python file imports it, the value of__name__will be the name of that file, so main() isn't called. It's clunky and not that efficient, but, 1, it works, and 2, if you cared about efficiency, you wouldn't be writing it in Python.Scubus@sh.itjust.works · 12 pts · 1y
thats why i name my modules main.py
d00ery@lemmy.world · 2 pts · 1y
Really helpful explanation, thanks.
bastion@feddit.nl · 5 pts · 1y
All code needs to have an entry point.
For Python and some other languages, this is the start of the file.
For other languages, this is a special function name reserved for this purpose - generally, "main".
In the first kind of language, the thought process is basically: I have the flow of execution, starting at the top of the file. If I want to make a library, I should build the things I want to build, then get out of the way.
In the other kind of language, the thought process is basically: I am building a library. If I want to make an executable, I should create an entry point they the execution starts at.
The debate is honestly pretty dumb.
_stranger_@lemmy.world · 2 pts · 1y
Python doesn't need the name main check to function at all. that's just a convenience feature that lets developers also include arbitrary entry points into modules that are part of a library and expected to be used as such. If you're writing a script, a file with a single line in it reading
print("hello world")will work fine when run:python thescript.pybastion@feddit.nl · 1 pts · 1y
Yes, because
Note the "I have the flow of execution", and the "if I want to build a library".
If you just want to build an executable, do as you wish, you already have the flow of execution.
If you want to build a library, make the relevant classes and functions and get out of the way (i.e., no IO, no long-running tasks).
If you want to combine them, use the main name check - or, make a package and do entry points that way. Either way works, because both can fulfill the goal of staying out of the way of those importing this as a library.
JATtho@lemmy.world · 18 pts · 1y
I would put my code in a
def main(), so that the local names don't escape into the module scope:(I didn't see this one yet here.)
YourShadowDani@lemm.ee · 5 pts · 1y
I'm a little new to Python standards. Is this better or worse than putting the
def main():outside the if statement (but callingmain()inside it)JATtho@lemmy.world · 5 pts · 1y
I intended this an sarcastic example; I think it's worse than putting the main outside of the branch because of the extra indent-level. It does have an upside that the
main()doesn't exist if you try import this as an module.myotheraccount@lemmy.world · 6 pts · 1y
I thought confusion about indent levels was the whole point of using python
mexicancartel@lemmy.dbzer0.com · 2 pts · 1y
But it feels like main function should not be indented
Sinthesis@lemmy.today · 17 pts · 1y
I use if__name__main__ often when working with AWS Lambda, but I also want to run it locally. Lambda wants to call a function with the params
eventandcontext. So I would do something like this:Tetragrade@leminal.space · 5 pts · 1y
Diabolical
bastion@feddit.nl · 13 pts · 1y
isn't that just normal usage? ..or, did I just whoosh and you were sarcastically saying that?
fruitcantfly@programming.dev · 6 pts · 1y
It is normal usage. Though personally I'd probably make another "main" function, to avoid declaring a bunch of global variables
nickwitha_k@lemmy.sdf.org · 2 pts · 1y
Yeah. I like using
main()that way too. It's usually just a high-level function that handles globals relevant to running in standalone and calling other functions to do work.bastion@feddit.nl · 1 pts · 1y
indeed.
diemartin@sh.itjust.works · 14 pts · 1y
Not that you'll ever use it. No, seriously.
Edit: actually, they are not quite equivalent. This code just checks whether we are outside any function, not necessarily in the main file (i.e. not in a module). I don't think there's an equivalent to Python's
__name__in stock Lua.qx128@lemmy.world · 11 pts · 1y
Nothing prevents you from putting a call to “main()” in the global scope
jacksilver@lemmy.world · 16 pts · 1y
The point of the name==main logic is that it checks if that is the file that was invoked (like running
python filename.py). If you just put a main() in the global scope it will be called either when the file is invoked or loaded (which can cause unintended consequences).littlewonder@lemmy.world · 2 pts · 1y
Dumb person question: if it's good practice to do this so things don't go sideways, shouldn't it be a built-in feature/utility/function/whatever?
jacksilver@lemmy.world · 2 pts · 1y
It is "built-in" as the name is part of python. However, Python runs top to bottom, rather than having a special entrypoint. So name is just a utility you can use in your design.
While it can be a good practice to define a main entrypoint, that's more of a design decision and not hard rule. Many applications would not benefit from it because there is only one way to actually call the application to begin with.
Edit: Also not a dumb question. All programming languages have unique elements to them due to how they were envisioned and or developed over time (Pythons 30 years old)
littlewonder@lemmy.world · 2 pts · 1y
I really appreciate the explanation!
Static_Rocket@lemmy.world · 7 pts · 1y
Just cross your fingers nobody attempts to import it...
bastion@feddit.nl · 1 pts · 1y
Due to the oneness of all things, I refuse to distinguish between library code and executable code. One and Zero are arbitrary limitations.
barsoap@lemm.ee · 11 pts · 1y
Did you know that
mainis usually a function?match@pawb.social · 10 pts · 1y
"pythonic"
jjjalljs@ttrpg.network · 8 pts · 1y
Call the function from the if block.
Now your tests can more easily call it.
I think at my last job we did argument parsing in the if block, and passed stuff into the main function.
driving_crooner@lemmy.eco.br · 6 pts · 1y
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 pts · 1y
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.dariusj18@lemmy.world · 5 pts · 1y
The if block is where my arg parser goes
lmmarsano@lemmynsfw.com · 5 pts · 1y
Alternative: put entry point code in file
__main__.py& run the containing package (eg,some_package) as a top-level expression (eg,python -m some_package).nickwitha_k@lemmy.sdf.org · 2 pts · 1y
TIL. Thanks for that!
iAvicenna@lemmy.world · 5 pts · 1y
wait till you see
HiddenLayer555@lemmy.ml · 4 pts · 1y
Luckily Python is one step ahead:
Also TIL that
:=is a thing in Python.iAvicenna@lemmy.world · 1 pts · 1y
yea I also couldnt get the formatting to work right, triple quotes kept turning things into accented letters, so I gave up.
and also := also known as the walrus operator is very fun and sometimes very convenient to use
10001110101@lemm.ee · 3 pts · 1y
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 pts · 1y
Nah
selfis quite important. The main part of a method is to access the state of the object.selfis just the interface to it.10001110101@lemm.ee · 4 pts · 1y
Guess I just prefer languages that do it this way:
Though I suppose confusion and bugs can happen when you do something like:
onlinepersona@programming.dev · 3 pts · 1y
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
barsoap@lemm.ee · 20 pts · 1y
Way too long an answer for a lemmy post
Depends on OS. Linux will look at the first bytes of the file, either see (ASCII)
#!(called a shebang) or ELF magic, then call the appropriate interpreter with the executable as an argument. When executing e.g. python, it's going to call/usr/bin/envwith parameterspythonand the file name because the shebang was#!/usr/bin/env python.Compiled C programs are ELF so it will go through the ELF header, figure out which
ld.soto use, then start that so that it will find all the libraries, resolve all dynamic symbols, then do some bookkeeping, and jump to_start. That is, it doesn't:mainis a C thing.Absolutely.
ld.sois an example of that.. Actually, wait, I'm not so sure any more, I'm getting things mixed up withlibdl.so. In any caseld.sois an executable with a file extension that makes it look like a library.EDIT: It does work. My (GNU) libc spits out version info when executed as an executable.
If you want to start looking at the innards like that I would suggest starting here: Hello world in assembly. Note the absence of a
mainfunction, the symbol the kernel actually invokes is_start, the setup necessary to call a Cmainis done bylibc.so. Don't try to understand GNU's libc it's full of hystarical raisins I would suggest musl.onlinepersona@programming.dev · 2 pts · 1y
How does that work? There must be something above
ld.so, maybe the OS? Because looking at the ELF header,ld.sois a shared library "Type: DYN (Shared object file)"The program headers don't have interpreter information either. Compare that to
ls"Type: EXEC (Executable file)".It feels like somewhere in the flow there is the same thing that's happening in python just more hidden. Python seems to expose it because a file can be a library and an executable at the same time.
Anti Commercial-AI license
barsoap@lemm.ee · 1 pts · 1y
Your ld.so contains:EDIT: ...with which I meant, modulo brainfart: My
libc.so.6contains a proper entry address, while other libraries are pointing at0x0and coredump when executed.libc.sois a linker script, presumably because GNU compulsively overcomplicates everything....I guess that's enough for the kernel. It might be a linux-only thing, maybe even unintended and well linux doesn't break userspace.
Speaking of, I was playing it a bit fast and loose:
_startis merely the default symbol name for the entry label, I'm sure nasm and/or ld have ways to set it to something different.JATtho@lemmy.world · 1 pts · 1y
Btw,
ld.sois a symlink told-linux-x86-64.so.2at least on my system. It is an statically linked executable. Theld.sois, in simpler words, an interpreter for the ELF format and you can run it:Which seems to be contained in the only executable
sectionsegment ofld.soEdit: My understanding of this quite shallow; the above is a segment that in this case contains the entirety of the
.textsection.MajorasMaskForever@lemmy.world · 5 pts · 1y
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@lemmy.blahaj.zone · 3 pts · 1y
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:
Edit: added example
Cratermaker@discuss.tchncs.de · 2 pts · 1y
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.
namingthingsiseasy@programming.dev · 1 pts · 1y
There are a lot of other helpful replies in this thread, so I won't add much, but I did find this reference, which you could read if you have a lot of free time. But I particularly liked reading this summary:
joyjoy@lemm.ee · 2 pts · 1y
Its called
runpy.run_scriptvga@sopuli.xyz · 2 pts · 1y
Shouldn't the third panel just be empty?
BuboScandiacus@mander.xyz · 1 pts · 1y
It’s fine like this
JoYo@lemmy.ml · 0 pts · 1y
main.pyor did you not read the manual?MousePotatoDoesStuff@lemmy.world · 0 pts · 1y
Depends on how lazy I am at the moment.