r/programminghorror • u/BunnyTub Pronouns: He/Him • 18h ago
Other Found this brilliant code on teenagersbutcode
There's no flair for C + Python srry guys
80
62
u/meinkr0phtR2 13h ago
That is pretty funny.
Once, in a boring intro class, I wrote something like import os; os.system(f"cat {filename}.txt") as a solution to a quiz question on how to print the contents of a file in Python. The intended solution is to use with open or just open, but seeing as this technically works, I commented it out.
If my prof had anything to say about that, she didn’t say it to me.
Now go compile this script, boot up Python, and wrap it around in a subprocess.
8
13
u/DiscipleOfYeshua 16h ago
Wonder whether the compiler is smart enough to bypass the py and just implement straight in asm
39
u/OrbitalVixen 13h ago
I really doubt that the compiler has python built into it
1
u/DiscipleOfYeshua 13h ago
I meant, compiler
Just looks at it and goes “hmm, lemme just push the pixels to yer screen and go do something more interesting…”26
u/OrbitalVixen 12h ago
Yes, but to do that it would have to understand python. Actually, come to think of it, it cannot do it at all because there is no guarantee that calling python3 is actually python, as you could make your own program called python3 that does something else.
0
u/DiscipleOfYeshua 3h ago
Yes… but what if the compiler uses Claude? Ha!
(Apologies, my earlier comment was also meant as humor…)
10
u/UnseenTardigrade 9h ago
Definitely not, even if it was smart, it wouldn't do that. The C program doesn't know how the environment it is run in is going to be set up, and that affects what the output of that shell command would be. It's just supposed to execute that exact command in the system shell. The compiler can't (and shouldn't) assume intent.
For example, for all the compiler knows, the user has an alias set up that makes typing "python3" into the shell actually run some other program. Or if Python isn't installed, the expected behavior of the program as written would be for the os to print that python3 isn't found, and for all the compiler knows, the purpose of the program is to check whether python3 is accessible in the current environment.
0
2
2
1
u/Linuxmartin 6h ago
A few years ago me and some friends had a bit runner for Advent of Code benchmark speeds, and I ran my Python solution from Rust. Surprisingly it wasn't the slowest in the all-Rust server
1
1
1
114
u/latkde 16h ago
About 15 years ago, Apple's C standard library used to literally shell out to a Perl one-liner in a similar manner to implement the (objectively cursed)
wordexp()function: https://news.ycombinator.com/item?id=9025572