572
submitted 5 days ago by [email protected] to c/[email protected]
top 50 comments
sorted by: hot top new old
[-] [email protected] 124 points 5 days ago

For optimal performance, you should rewrite it in Rust:

inline_python::python! {
    print(js2py.eval_js("(number) => number % 2 ? 'odd' : 'even'")(number))
};
[-] [email protected] 70 points 5 days ago* (last edited 5 days ago)

And now you can use wasm to run it in a browser!

[-] [email protected] 25 points 5 days ago
[-] [email protected] 12 points 5 days ago

Make sure the browser is made using Rust and run on a VM running on Linux, compiled to WASM.

[-] [email protected] 139 points 5 days ago
print("odd" if num % 2 else "even")

That's the native python version, for those curious

[-] [email protected] 87 points 5 days ago

The ternary syntax is really my only real gripe with python design -- putting the conditional BETWEEN the true and false values feels so very messy to me.

[-] [email protected] 22 points 5 days ago* (last edited 5 days ago)

Eh, reads pretty naturally to me. That said, (like I lisp)

[-] [email protected] 15 points 5 days ago

Lisps makes more sense to me though

(if condition a b)

VS

a if condition else b

[-] [email protected] 9 points 5 days ago

I was more talking about (+ a b) and such.

load more comments (2 replies)
[-] [email protected] 12 points 5 days ago

It's kinda natural to me having used Perl a lot.

[-] [email protected] 21 points 5 days ago

That's not quite the argument you might think it is

load more comments (10 replies)
load more comments (5 replies)
[-] [email protected] 4 points 5 days ago

At least you guys have ternary syntax cries in kotlin.

[-] [email protected] 2 points 4 days ago

It's really special to not have ternary, but have Elvis.

[-] [email protected] 9 points 5 days ago

That's way too non-convoluted enough

[-] [email protected] 17 points 5 days ago

Python is kinda like that in general, unless you try to make it read like ass

[-] [email protected] 6 points 5 days ago

You would not believe the number of people I’ve interviewed who excel at making Python read like ass.

[-] [email protected] 6 points 5 days ago

I mean, it does have enough ways to write the same thing that it can really allow for some funny code golf, but some people just have no sense of readability whatsoever.

[-] [email protected] 5 points 5 days ago

Clearly an inferior language. /s

[-] [email protected] 7 points 5 days ago
[-] [email protected] 18 points 5 days ago

I think the idea is it reads more naturally, so you can read it like this return A if statement is true else return B

[-] [email protected] 1 points 3 days ago

Is it really more natural for a non-programmer than "if statement is true than a else b"? I can't evaluate because of decades of C, so for me the python logic is still bizarre.

[-] [email protected] 1 points 3 days ago

Maybe?

For C at least it doesn't have the actual words, so you need to know what the specific symbols are var = condition ? a : b. In that expression we don't know what a or b are in regards to the condition.

Python literally is a if condition else b, so it reads out what is being done.

load more comments (1 replies)
[-] [email protected] 9 points 5 days ago* (last edited 5 days ago)

Edit... I reread your comment and realized that python does it differently and that everything I typed was irrelevant... I'm still gonna leave it if anyone is interested in ternary expressions, but I suppose the answer to your question is, that's just how python does it.

That's how ternary operators are designed to work. In essence, if you're looking to do a single line if/then, you can directly assign a variable from the result of a ternary expression.

As an example, I was scripting something earlier where there may or may not be a value returned from a function, but I still had to do something with that return value later. For this thing, I was using JavaScript.

I ended up with:

return platform == "name"  ? "Option 1" : "Option 2"

If I were to write that out in a typical if/then it would be:

if (platform == "name") {
    return "option 1"
} else {
    return "option 2"
}

A ternary starts with a boolean expression, then the if true value, else the false value. That's returned to either a variable or if in a function like my example, to the object calling the function. It's just a way to write less code that in many cases is easier to read.

[-] [email protected] 3 points 4 days ago

Oh wow, I think I hate that... Condition between the results? Yuck.

[-] [email protected] 57 points 5 days ago

Please. That's C's ternary operator. JS is just a pile of garbage cosplaying as a programming language

[-] [email protected] 4 points 5 days ago

Why do you say it's a pile of garbage?

[-] [email protected] 19 points 5 days ago

Because of all the garbage

[-] [email protected] 7 points 5 days ago

Clearly the garbage collector is too effective

[-] [email protected] 4 points 5 days ago

No they're not supposed to be piling it up

[-] [email protected] 7 points 5 days ago

Is a garbage collector not a garbage disposal. Smh.

load more comments (1 replies)
[-] [email protected] 38 points 5 days ago

print( ["even", "odd"][num % 2] )

If you need to avoid evaluating the wrong branch:

print( [lambda: "even", lambda: "odd"][num % 2]() )

[-] [email protected] 8 points 5 days ago
[-] [email protected] 11 points 4 days ago

Not as cursed as

print("eovdedn"[n%2::2]) 
load more comments (1 replies)
[-] [email protected] 18 points 5 days ago

Just send pseudo code to AI and compile straight to binary.

[-] [email protected] 12 points 5 days ago

Peak programming

[-] [email protected] 11 points 5 days ago

I love something = condition and result1 or result2 in lua

[-] [email protected] 10 points 5 days ago

Yeah... I played that "serial killer or programming language inventor" game.

The only one I was completely in disagreement with was the inventor of Python. He's definitely a mass-murderer

[-] [email protected] 6 points 5 days ago* (last edited 5 days ago)

Are you sure it isn't just that he's Dutch?

load more comments
view more: next ›
this post was submitted on 12 Jun 2025
572 points (98.6% liked)

Programmer Humor

24287 readers
638 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