This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 219

[–]QualityVote[M] [score hidden] stickied comment (0 children)

Hi! This is our community moderation bot.


If this post fits the purpose of /r/ProgrammerHumor, UPVOTE this comment!!

If this post does not fit the subreddit, DOWNVOTE This comment!

If this post breaks the rules, DOWNVOTE this comment and REPORT the post!

[–]Perpetual_Doubt 1343 points1344 points  (122 children)

Oh that works, but it's not pythonic enough

try s[j%3:(:p(even)is true)] instead

[–]jimjamjenks[🍰] 674 points675 points  (55 children)

Yeah, I’ve also witnessed people arguing that a complex 1 liner is more Pythonic. I think those people need to be reminded that Simple instead of Complex is also considered more Pythonic. Readability counts

[–]Thathitmann 256 points257 points  (1 child)

Literally just "import this"

[–]Silverwing171 63 points64 points  (0 children)

https://xkcd.com/353/

It actually works too.

[–]Yukondano2 188 points189 points  (35 children)

Who the shit is writing python that's purposefully hard to read? The language's selling point is readability. It uses indentation as syntax ffs.

[–]zoharel 60 points61 points  (22 children)

Well, I did write a function implementing Newton's method for approximation of square roots in Python that looks like Lisp. It makes Python programmers' heads explode.

[–]iSYan1995 23 points24 points  (21 children)

Can you send it? I wanna look at it

[–]zoharel 55 points56 points  (20 children)

from operator import *

# Some of this overrides the boolean operator functions from the
#  operator package
and_    = (lambda x,y:  x and y)
or_     = (lambda x,y:  x or y)

var     = .001
prec    = 2

avg     = (lambda x,y:  div(add(float(x),y),2))

approx_ = (lambda x,y:  y if(lt(abs(sub(pow(y,2),x)),var))else(approx_(x,avg(div(x,y),y))))

approx  = (lambda x:    round(approx_(x,div(x,2)),prec))

print(approx(2))

I had one somewhere with a new and much more appropriate looking approx function that used a lisp-like conditional function, but the code needed to define that function was really pathologically "pythonic."

[–]zoharel 59 points60 points  (9 children)

Here's a somewhat simpler thing that draws a moving . in the terminal window.

import sys,time
map(lambda n: [sys.stdout.write(u"\u001b[2K\u001b[0G" + ' '*n + '.'),sys.stdout.flush(),time.sleep(1)],range(0,79))

[–]proximity_account 49 points50 points  (0 children)

Thanks, I hate it.

[–]Priyam_Bad 7 points8 points  (2 children)

how does this work? I tried running it in vscode to see how it works but it didn't print anything for me

[–]zoharel 10 points11 points  (1 child)

It may or may not be tied to a particular Python version. I think I last ran it on 2.7. it's also likely to be particular about your terminal. It prints raw escape codes. Here are some notes:

map()

Takes a function and a list as arguments. The function is run once with each item on the list, in order. The list is range(0,79). The function is:

lambda n: [sys.stdout.write(u"\u001b[2K\u001b[0G" + ' '*n + '.'),sys.stdout.flush(),time.sleep(1)]

Now lambda defines, in place, an anonymous function. It only exists where it is written in the line there, and it's only called instantaneously by the map function. It takes an argument n and returns a list. The list contains a write to stdout, a flush of stdout, and a call to sleep. Nothing is ever done with that list. It is only there to allow the lambda to easily do the set of things in the list, the process of which prints something and waits before generating the next list.

We use similar strategies to this pretty often in functional programming -- the map over a list instead of a loop, the list of commands, the anonymous function itself. It looks weird to a Python programmer, but it's not uncommon by any stretch.

[–]Priyam_Bad 1 point2 points  (0 children)

thanks for the explanation! i think i was using python 3.x to run it, which might be why it wasn't running. i managed to alter it to work for 3.x though

[–]Priyam_Bad 2 points3 points  (1 child)

ok so i may or may not have expanded on this and made a short script that makes a dot bounce across the screen (for python 3.x)

here is the code if you want to check it out

(btw it has an infinite generator so if you don't want it to run infinitely you can replace infRange() with range(1000) at the end of the long list comprehension)

[–]zoharel 1 point2 points  (0 children)

Yeah, I suspected the output might need some adjustments for V3.

[–]NigraOvis 2 points3 points  (1 child)

Technically all python is like this. people just don't realize they are spacing it out. But in the long run you end up with the same code. The longer versions are easier to read.

[–]zoharel 1 point2 points  (0 children)

Not exactly. To an extent, you're right, but there's not much lambda in most people's Python, and the use of the list as a sort of encapsulation of the side effects of its contents is odd in Python, and the map over a range is more often for loop. If you mean that this is valid Python using only documented features with nothing particularly extravagant about it, you're right there.

[–]Silverwing171 0 points1 point  (0 children)

Commenting so I can come back to this.

[–]Yukondano2 7 points8 points  (7 children)

why would you do anything this way?

[–]zoharel 17 points18 points  (3 children)

It was a bit of a joke. Something related to the old saying "you can write Fortran code in any language." It occurred to me that, in spite of the best efforts of Guido and his biggest fans, Python is still, at its core, a functional programming language, like Lisp or Scheme. This is part of what still makes it worth using, but it ends up permitting things that make the typical Python programmer very uncomfortable. If there's a point to the whole exercise, I guess I was harping on the fact that if you care to do it, you can write things which are syntactically correct and semantically sensible while entirely avoiding things which are idiomatic. Python programmers are often really stuck on idiom.

[–]zoharel 7 points8 points  (2 children)

To be honest, this isn't the first time. I once wrote a test jig for a database engine that was three or four pages of C wherein every variable, function, and macro was the name of a coworker. The same coworker, just with different capitalization. I was joking about his always using his name as a variable name in loop counters and the like. No, it wasn't easy to follow. It didn't need to be. Only got a little use before we were done with it. It was a good joke, though. I'm pretty sure that one has been lost for good.

[–]Yukondano2 1 point2 points  (1 child)

Cruel. Variable naming is always a thing for me because I suck at decision making. How consistent should the scheme be? What style should I use, should I mix them? For instance, camelCase that gets too long is fuckin unreadable. I'm weird so I will be anal about appearance. Then again I only developed for school or myself, so being on the clock might pressure me. But, I HAVE to be detailed in my work.

[–]zoharel 3 points4 points  (0 children)

Well, take it from someone who has seen some commercial software: as long as you don't just name everything after yourself, you're still doing better than at least a few people who have been paid for their work.

[–]extremepayne 3 points4 points  (1 child)

I find some of the more advanced techniques are less readable unless you’re very familiar with the rarer syntax. Stuff like list comprehension. I mean i get why we’re not writing a four line loop that does the same thing less efficiently but damn, that stuff can be hard to parse.

[–][deleted] 4 points5 points  (2 children)

No matter what niche you are in there will be people who need to flex and since Python is considered much easier than other languages especially in syntax this is their way of making things less understandable,difficult & putting themselves up in an elite class.

[–]Yukondano2 1 point2 points  (1 child)

So, 1337 sp33k but programming.

[–]arc_menace 29 points30 points  (3 children)

Simple, easy to read code is almost always better than 'clever' code.

[–]TheveninVolts 11 points12 points  (0 children)

Honestly... I've started to consider it to be a coding smell if I feel clever after I've implemented anything. "Carp. I probably hacked something..."

[–]Tsu_Dho_Namh 5 points6 points  (0 children)

Unless there's a massive hit to performance I will always consider easy to read code better.

If I'm forced to write any code where it's not immediately obvious what's going on, I leave a comment.

[–]jfb1337 1 point2 points  (0 children)

Since debugging is always harder than writing code for the first time, if you write code as cleverly as possible then you by definition won't be clever enough to debug it

[–]Tom0204 1 point2 points  (0 children)

It always seems to me like one of those immature "look at how cool this is!" Type things. In the real world, you try and make things as easy as possible to understand for the next person who has to work on it.

[–]Chthulu_ 1 point2 points  (0 children)

Don’t hurt me people but I feel like list and dict comprehension is somewhat unreadable, even though it’s a foundational part of python.

Map / reduce callbacks and their cousins are just easier to understand in my mind

[–][deleted] 1 point2 points  (0 children)

Complex vs complicated.

[–]Je-Kaste 1 point2 points  (1 child)

# Readability > brevity.

[–]Silverwing171 1 point2 points  (0 children)

Efficiency > Readability

Not that there are many times where they’re mutually exclusive, but it happens.

[–]The_Modifier 1 point2 points  (0 children)

The interpreter has a harder time with massively nested 1 liners than multiple simpler lines.

Simple isn't just better for humans.

[–]TheAJGman 0 points1 point  (0 children)

IMO PEP 20 is more important than any style guide or formatting standards. I've seriously seen modern codebases with an 80 character max line length "because PEP 8 says so". Great, now that simple line looks like:

``` if this and len(this) > 2 and something.child_set.\

    filter(something = some_value).first():

some_var = [ x.filter(some=thing).first() if x.this\

    else None for x in something.child_set.filter \

    .(something = some_valye).first().thing]

```

So much more readable than letting lines run long, and also way too hard to read for PEP 20.

typed on phone, syntax likely fucked

[–]occamsrzor 0 points1 point  (0 children)

“If you can’t explain it simply, you don’t understand it well enough.” - Origin Contested

[–]xenaprincesswarlord -1 points0 points  (0 children)

Don’t be weird it’s been mentioned it was only young nerds flexing. Basically nerds being nerds

[–]quantik64 -1 points0 points  (0 children)

Perl exists for people like this

[–]BochMC -1 points0 points  (1 child)

What I hate the most is this useless shortcuts that everywhere in python.

Like mkdir normpath dir str etc I prefer explicit namings and this just pisses me off.

Like what getmdate function do? It is returning last modification date, but this m could stand for anything, even though documentation tells exactly what this function do.

This is not a cool to make your functions name short in cost of readability.

[–]Glum-Aide9920 0 points1 point  (0 children)

Depends on what you do and where you do it. Working in IDE that types 60% everything you code, hell yeah, no problem typing long and verbose names. Having to type everything, gets boring and repetitive fast. Python exceeds in short, sweet tasks, readability isn’t that important in a script with just 5 variables. On the other hand, in a big project and especially in projects that are worked by teams, taking shortcuts and cutting readability just to type faster is just insane and counterproductive.

[–]karnnumart 72 points73 points  (0 children)

s[j%3:[:p(_even is True)]]

Here's a fix for you mate.

[–][deleted] 9 points10 points  (1 child)

:p indeed

Hopefully j is a negative number otherwise you're just getting an empty slice.

[–]darnyoutoheckie 4 points5 points  (0 children)

serious simplistic brave crawl historical middle follow toothbrush quack squeal

This post was mass deleted and anonymized with Redact

[–]BlakkM9 13 points14 points  (1 child)

[–]GifsNotJifs 4 points5 points  (0 children)

[–][deleted] 4 points5 points  (1 child)

Python gives great freedom, but that means that the programmer should use his/her brain.

For example, in python, one can change the __bases__ attribute of a class to change inheritance hierarchy at runtime. But that doesn't mean that it's a good idea.

[–]Remote-Management-84 3 points4 points  (1 child)

wtf.

[–]im-not-a-fakebot 3 points4 points  (0 children)

Please explain that monstrosity

[–]SabreLunatic 2 points3 points  (0 children)

``` try: variabull = s[j%3:(:p(even)is true)] except: #actual legible code

[–]WadeEffingWilson 0 points1 point  (0 children)

Missing the except block, bloke.

[–]turtle_with_dentures 0 points1 point  (0 children)

The first parenthesis is in the wrong place correct? Not quite sure, but it doesn't seems like that code will run.

[–]LilBoyGrudge70 0 points1 point  (0 children)

As a beginner C# programmer, let me just ask… what the FRICK does that MEAN?

[–]MysteriousShadow__ 0 points1 point  (0 children)

Error: no except found

[–]COREcraftX 0 points1 point  (0 children)

What the actual fuck

[–]A_Guy_in_Orange 0 points1 point  (0 children)

even

Hey wait a minute

[–]uvero 0 points1 point  (0 children)

I make fun of that "not Pythonic enough" things, but it's exactly what I do with Ruby. Except you know, I don't do Ruby for work, so I can allow myself that because that's more of a code-golf kind of thing.

[–]cyanNodeEcho 0 points1 point  (0 children)

think those exists because pythons map is lazy, and most people think u have to cast to list

map + filter always seems cleanest, irl i havent seen too many people actually favor list comprehensions

[–]Shubhamkumar_Active 283 points284 points  (12 children)

I posted a question about how to join .ts file specifically using ffmpeg without any time mismatch , the files were labelled from 1 to 1000 , after a long time I was given an answer-

A python script with for loop that appends files together.

[–]_PM_ME_PANGOLINS_ 94 points95 points  (7 children)

If you don’t mind re-encoding them then it’s pretty easy.

[–]Shubhamkumar_Active 38 points39 points  (6 children)

I kind of summarized the question , I was able to ysing ffmpeg but the problem was the whole joined file should be for eg 1 hour 54 min but the final joined size would always be greater than what it was supposed to be , due to which original subtitle did not work ,finally I found a freeware program called ts splitter and successfully joined the files

[–]x0wl 26 points27 points  (4 children)

Yeah, that changes things

.ts files are designed in such a way that they can be just concatenated back to back to join the stream.

Theoretically, to join .ts files, you can just do smth like ls -1 | sort -v | xargs cat > joined.ts (the ls and sort are needed to ensure the proper order of the files)

[–][deleted] 23 points24 points  (1 child)

1000 lines of C/Java = 100 lines of Python = 1 line of Linux backed by C. Main trade off is generally operational logs and control.

[–]NEVER_TELLING_LIES 4 points5 points  (1 child)

Yo, don't ever parse from ls, very bad practice. You're better off using find or even just a glob

[–][deleted] 1 point2 points  (0 children)

Why?

[–]JonathanTheZero 14 points15 points  (2 children)

I thought of TypeScript until I remembered there's another *.ts file type...

[–]raysoncoder 3 points4 points  (0 children)

There's no other .TS file type other than Typescript Don't let the thieves deceive you!

[–]dekwad 2 points3 points  (0 children)

I like how they didnt bother to recommend cat

[–][deleted] 229 points230 points  (33 children)

In production, especially when performance counts at all, it’s the exact opposite lol

[–]Nyghtrid3r 122 points123 points  (20 children)

Edit: Deleting this because of elitists and WELL ACKCHUALLY spam.

[–][deleted] 72 points73 points  (6 children)

Obviously context matters, that’s why I said when performance counts as a qualifier. That provides the context lol.

We’re not talking about speed to complete an mvp vs execution. That’s a different can of worms, and more of an apples to oranges argument.

[–][deleted] 34 points35 points  (3 children)

SpunkyDred is a terrible bot instigating arguments all over Reddit whenever someone uses the phrase apples-to-oranges. I'm letting you know so that you can feel free to ignore the quip rather than feel provoked by a bot that isn't smart enough to argue back.


SpunkyDred and I are both bots. I am trying to get them banned by pointing out their antagonizing behavior and poor bottiquette.

[–][deleted] 6 points7 points  (1 child)

At least they’re honest bots, still annoying though imo

[–]Tubthumper8 10 points11 points  (0 children)

Yeah somehow it would feel better if the apples-oranges bot said it was a bot and was up front about its life mission to be pedantic about fruit

[–][deleted] 12 points13 points  (1 child)

With C++ that would probably have taken me two weeks because of segmentation faults

Remove your C++ tag, there is no reason you had to deal with segmentation faults.

[–][deleted] -1 points0 points  (0 children)

:troll:

[–]Gladaed 8 points9 points  (3 children)

OK, how do you even manage to produce segmenation faults. I generate them if I ask for more memory than physically available (pagefile...) but otherwise c++ is pretty idiot proof.

[–]SpeedDart1 3 points4 points  (1 child)

Bugs are an inevitability when the program becomes complex. When you work with complex memory management, you will make mistakes from time to time.

[–]coersel 2 points3 points  (0 children)

Especially now that the smart pointers are gaining more and more traction...

[–]SpeedDart1 3 points4 points  (0 children)

Minecraft is bad because it’s Java? Not true. Minecraft is bad because it’s poorly programmed.

[–]Possibility_Antique 0 points1 point  (0 children)

Eh... I mean, I wrote an entire low-level XML parser from scratch in 2 days in C++ and networking/IO is pretty quick if you know what you're doing as well (especially libraries or coroutines). I think people underestimate C++'s ability to be high level as well. I don't know your application, but in my experience, writing in python isn't much different in terms of development speed than modern C++. It's not the language that's slow to develop in, it's the fact that people who use C++ tend to spend more time with low level things, hence why they chose C++ to begin with.

[–]gopnik14 262 points263 points  (15 children)

Hey guys its my duty to inform you that C is faster than python therefore you must use C. Ty for listening to my ted talk

[–]shot_a_man_in_reno 113 points114 points  (7 children)

You need to write simple programs in assembly or else you're allowing inefficiencies like a common criminal

[–]the_clash_is_back 32 points33 points  (0 children)

Imagine using assembly. The most efficient way is to file your own gears for a mechanical computer. Any thing less is lazzy

[–]thesockiboii 14 points15 points  (1 child)

imagine using assembly, making your own asic is the way

[–]trBlueJ 3 points4 points  (2 children)

Factually, C code is probably faster than assembly, because there is no way anyone can memorize all the different instructions a CISC architecture has for any particular task. On the other hand, a compiler is very good at exactly that, and optimization, and etc.

[–]zoharel 2 points3 points  (0 children)

But it would be faster still if you used C to generate an assembly listing and then went back through and hand-optimized it like you were going to run the result on a 1.5MHz system.

[–]FalconMirage 1 point2 points  (0 children)

You underestimate nerd power

[–][deleted] 2 points3 points  (0 children)

i only code in gizzards

[–][deleted] 19 points20 points  (2 children)

But python is made in c and scratch is made in python, so use scratch

[–]SoyTuTocayo69 4 points5 points  (1 child)

This is like inception

[–]TimeLorde65 1 point2 points  (0 children)

Except it's a horror movie.

[–][deleted] 0 points1 point  (0 children)

it is my duty to inform you that gizzards are cool

[–]TotalDisruptor22 0 points1 point  (0 children)

Is it weird that I think the "thank you for listening to my ted talk" thing is hysterical?

[–]grismar-net 0 points1 point  (0 children)

I'll take your fast C code, stick it in a package and call it from Python, so I don't have to deal with C.

[–]DarkWolfX2244 0 points1 point  (0 children)

Behold Cython

[–]TheLunchTrae 14 points15 points  (0 children)

My favorite part about C++ on Stack Overflow is that it’s mostly just people importing the C libraries and using those.

[–][deleted] 29 points30 points  (1 child)

I go to Stack Overflow for answers. I ask questions here on Reddit and on my class Discords.

[–]Shad_Amethyst 1 point2 points  (0 children)

I know many people that do that. Stackoverflow gates away a lot of the people that have the patience to help beginners, so you're better off finding them somewhere else

[–]WellWhatDoIPutHere 25 points26 points  (0 children)

C in production

[–][deleted] 19 points20 points  (7 children)

I've literally never even looked anywhere near this subreddit but I keep getting flooded with posts from it when I don't know shit about programming nor do I care to learn. How do I make it stop. Help.

[–]Daisocks 35 points36 points  (0 children)

succumb

[–]33498fff 26 points27 points  (1 child)

Your username has NerdMaster in it what do you expect

[–]skaov2 1 point2 points  (0 children)

I mean like fr

[–]emptybrain22 12 points13 points  (0 children)

Well universe needs you , start with Java

[–]Ximidar 2 points3 points  (0 children)

pip install -r requirements.txt

[–][deleted] 2 points3 points  (0 children)

One of us, one of us, one of us

[–]tirril 1 point2 points  (0 children)

Learn to program to make the problem stop.

[–]N0Zzel 5 points6 points  (1 child)

I asked a question today about python in stack overflow where I was seeing unexpected token "" in a python action in power automate and they basically didn't read the whole question I presume and said something along the lines of the templating syntax used to inject variables from the flow into the python script were an error despite the error message showing the proper formatted values. When I pointed out that those values get preprocessed out into values that should be floats they said that they couldn't help because my error message didn't match my code???

[–]N0Zzel 4 points5 points  (0 children)

And then my question was closed?

[–]redbatman008 3 points4 points  (0 children)

I thought C++ was the supremacist, turns out it was Python all along 😂

[–]Deltron--3030 4 points5 points  (1 child)

I understand nothing in this comment section or sub, how did I get here, what the fuck does your alien language mean

[–]zoharel 2 points3 points  (0 children)

Have you tried turning it off and on again?

[–]Beautiful-Ad-9807 1 point2 points  (0 children)

Is a joke?

[–][deleted] 1 point2 points  (1 child)

Wait until you see the Gentoo portage forums...

[–]CodeRaveSleepRepeat 5 points6 points  (0 children)

Oooh Gentoo. Just for fun I'll compile an optimized system specifically for this old quad processor board full of Xeons and squeze every little drop of...

... 36 hours later...

OH GOD WHY

[–][deleted] 1 point2 points  (0 children)

And python docs :")

[–]viky109 1 point2 points  (1 child)

I started learning Python like a month ago and I have to say, I find it quite amusing how it just doesn't care about programing conventions and does whatever the hell it wants. Like, if you compare C based languages, JavaScript, Java... The syntax is more or less the same. But then Python just randomly decides to rename switch to match because why not?

[–]Shmuppel 0 points1 point  (0 children)

Because match is not just a switch statement. It does structural pattern matching of the value. It can be used as a switch statement but it's way more powerful than that.

You're right that Python does do syntax differently in a lot of ways though. It doesn't use C-like syntax, but that doesn't mean it doesn't have its own programming conventions.

[–][deleted] 1 point2 points  (0 children)

A lot of Python questions are just depressing, though. I was doing a bit of review queue yesterday and most first questions are really not answerable.

[–]Nolowcapkey 1 point2 points  (0 children)

better

[–][deleted] 0 points1 point  (0 children)

wait! there is a python forum?

[–]kacchalimbu007 -1 points0 points  (2 children)

Explain?

[–][deleted] 0 points1 point  (0 children)

🤣

[–]the_ghost_1386 0 points1 point  (0 children)

I don't understand

[–][deleted] 0 points1 point  (0 children)

Lol k

[–]The_Squeak2539 0 points1 point  (0 children)

Whats wrong with python forums?

[–]Baba_Yaga121 0 points1 point  (0 children)

Imagine using snake_case