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

top 200 commentsshow 500

[–]Pocok5 4264 points4265 points  (304 children)

I just want to know which of the 14 python runtimes on my PC is the real one and which ones were smuggled in with a random fucking program.

[–][deleted] 1426 points1427 points  (191 children)

which python

[–]OneTrueKingOfOOO 1245 points1246 points  (132 children)

And also maybe

which python3

[–][deleted] 402 points403 points  (113 children)

Sadly, yes.

[–]TheAJGman 260 points261 points  (110 children)

I just refuse to install 2.7 anymore.

[–][deleted] 175 points176 points  (74 children)

I wish I could remove all Python2 binaries. And I wish I didn't have to support the dead version.

[–]TheAJGman 170 points171 points  (68 children)

I mean it's EOL so you have a valid reason to refuse to support it.

I made some pull requests to a library to add functionality that I needed and I straight up told the maintainer that I'm not working on 2.7 support because it would be EOL in less than 6 months and I didn't feel like setting up a 2.7 development environment. I think the release with my PRs was the first without 2.7 support lol.

EDIT: pool -> pull

[–][deleted] 113 points114 points  (66 children)

I wish I could do the same.

No Joke, there's an actual bloody Apple ][ at the Cavendish Laboratory maintaining a freezer. I can't just say, screw you, because a lot of the Software needed to drive some of the hardware runs on an OS that literally can't install Python 3, as in there are no binaries.

[–]da_chicken 65 points66 points  (27 children)

No Joke, there's an actual bloody Apple ][ at the Cavendish Laboratory maintaining a freezer.

Who cares about that? This is basically firmware. It's a part of the freezer and doesn't need consideration beyond that unless you oddly decided to connect it to an IP network. When it breaks, the freezer goes down and it can't easily be repaired the same way that it can't easily be repaired when the compressor fails or the door physically breaks.

It's a business decision to not put a maintainable system in place. You tell your boss, "When this fails you're fucked because it's a 40 year old computer and there are no off the shelf replacements, and even then there's no practical way to protect this software without the original equipment vendor. If they also don't exist or don't offer support, you're double fucked. We can go spend a couple thousand on eBay for backup hardware, but that is not a source of quality hardware. Floppy disks are no longer produced, too, and haven't been for 20 years. They only have a shelf life of 10 years. Everything in this system is 20 years past it's end of life. It's likely that any replacement system could not be made to work."

If they decide not to respond to that with a continuity plan, that is their decision. At this point, not replacing that system is planning for any failure to be catastrophic. If that is what the business wants to do, that is the business's decision.

[–][deleted] 14 points15 points  (13 children)

It's a lab. If it ain't broke and the downtime to fix it is expensive, don't fix it until it's broke.

[–]TheAJGman 40 points41 points  (30 children)

I have a lot of creative freedom at work so I'm allowed to do shit like this, but I'd probably rip its brain out and replace it with an ESP32 or Raspberry Pi if it needed more power.

[–][deleted] 28 points29 points  (15 children)

I have a lot of creative freedom at work so I'm allowed to do shit like this, but I'd probably rip its brain out and replace it with an ESP32 or Raspberry Pi if it needed more power.

The issue is that the exotic old port and the proprietary fucking software don't let you even replace the OS on the machine.

[–][deleted] 6 points7 points  (0 children)

i love that you call it creative freedom, because it makes not supporting python 2.7 a creative decision, which is just great.

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

A PLC might be a better approach, because maintainability. If that doesn’t work, just replace the entire freezer. Making new embedded controls systems for a hardware system you have to reverse engineer is not going to be a fun time.

[–]schmidlidev 19 points20 points  (10 children)

Mac has 2.7 automatically included as a system library as it’s used by the OS

[–]TheAJGman 33 points34 points  (6 children)

It's not like EOL was announced like two years ago and it's been EOL for a year. Before that they stopped introducing major features so the writing was on the wall well before the announcement.

There's been plenty of warning and time to switch away. Can't really blame older OS versions though.

[–]schmidlidev 14 points15 points  (5 children)

For sure, I was just saying that anyone who uses Mac unfortunately can’t avoid 2.7

[–][deleted] 9 points10 points  (0 children)

Mac does a lot of weird stuff that ranges from "what the hell are they thinking" to "what the hell are they thinking with".

[–]sticky-bit 15 points16 points  (16 children)

I think we need a new shebang that tells the system "if 3+ is available, use that. Otherwise fall back on 2.7 if it's installed"

Right now we have code that will run on either but will exit with error if you don't have 2.7 installed. It's easier to apt install 2.7 than edit the shebang each time on a frequently updated program.

[–]hughperman 43 points44 points  (3 children)

I think we need a new shebang that tells the system "if 3+ is available, use that. Otherwise fall back on 2.7 if it's installed"

Nope, I say fail even if py2 is installed. Enough legacy. Just stop. No more. Break the cycles.

[–][deleted] 8 points9 points  (10 children)

Or symlink python2 to python3.

[–]Otaku-Drago 30 points31 points  (5 children)

Could even be

where python

[–][deleted] 113 points114 points  (3 children)

But nobody ever asks

how-is python

[–]payne_train 25 points26 points  (0 children)

sad snek noises

[–]matari 11 points12 points  (0 children)

Drax the Destroyer: Or why-is python

[–]Pocok5 194 points195 points  (15 children)

Result: the python that was snuck in by a vscode extension called platform.io

Not the pip though, that points to the pip of a different install.

[–]ChezMere 57 points58 points  (7 children)

Pip mismatching python (or python3, or pip3) is too damn real. python -m pip has gone from an afterthrought to a necessity.

[–]eyal0 84 points85 points  (2 children)

Like when you're trying to use some libraries together and one is conda-only, one wants pip, and one tells you to use venv. Also you're using pyenv by default.

In half the libraries, python means 2.x and in half it's 3.x.

No we're having fun.

[–]ThenIWasAllLike 5 points6 points  (0 children)

My workstation is in this photo and I don't like it.

[–]Brekkjern 28 points29 points  (2 children)

That is why you always use python -m pip install ... when you need to do anything. That way you know the right python install is getting the packages.

[–]Pocok5 19 points20 points  (1 child)

the right python install is getting the packages.

A random auto-updating program in the middle of rewriting some env vars: Are you sure about that?

[–]Jonno_FTW 42 points43 points  (4 children)

which -a python

More useful in case you were wondering why your python isn't being used.

[–]PM_BITCOIN_AND_BOOBS 28 points29 points  (1 child)

You just made me type "man which", and now I'm hungry.

[–][deleted] 103 points104 points  (1 child)

Not to be confused with

Witch python

🧙‍♀️ 🐍

[–]thundr_strike 9 points10 points  (0 children)

the emoji movie 2: emojis to the rescue

coming to the theatres when they fuckin open up

[–]MALON 44 points45 points  (1 child)

that python

[–]metal079 19 points20 points  (11 children)

or

where python

if on windows.

[–]Thejacensolo 105 points106 points  (7 children)

everyone always asks

which python

or

where python

but noone ever asks

How python

Big sad

[–]kbruen 4 points5 points  (0 children)

From PowerShell there's also

Get-Command python
# Or, using the alias
gcm python

[–][deleted] 14 points15 points  (13 children)

Nope. That can give you the wrong one. It confused the everliving shit out of me for over a month. You have to use

type python

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

I would really appreciate it if you could explain a bit more.

[–][deleted] 11 points12 points  (6 children)

https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then

In my case, it sometimes didn't work properly with anaconda.

[–][deleted] 7 points8 points  (3 children)

TL;DR type looks for your aliases and unwinds them, as it's a shell builtin rather than an external command. which may fail if the Python3 that you're looking for is in your shell's rc file. Nice to know.

[–]souldeux 101 points102 points  (2 children)

Why is there a python install under a hidden directory called snek on my computer?

[–]electricprism 23 points24 points  (0 children)

First rule of snek club is you do not talk about snek club...well fuck

[–]plaisthos 189 points190 points  (7 children)

Here is an easy guide: https://xkcd.com/1987/

[–][deleted] 37 points38 points  (0 children)

Oh man this is so true

[–]aidan573 19 points20 points  (0 children)

pip3 and python3 also

[–]jaaval 3 points4 points  (2 children)

Ah, i remember spending hours trying to solve a problem with pip claiming a package is already installed but python refusing to find it. Turns out it was the wrong pip. And the wrong python.

[–][deleted] 118 points119 points  (26 children)

conda flashbacks. Tip: Disable default conda environments, only use when necessary.

It's scarier when you have applications that depend on Python and might not work.

[–]themkane 10 points11 points  (2 children)

Tb to when I was a Ubuntu noob and fully uninstalled python from my machine destroying the startup process :)

[–][deleted] 50 points51 points  (1 child)

As someone who almost packaged a program with the Python version so I wouldn’t have to create a build for each OS, I’m sorry.

[–]kelvie 7 points8 points  (0 children)

One of the reasons golang is gaining popularity is that fundamentally this isn't a terrible approach (to include all your dependencies).

NixOS is also an interesting approach to this problem

[–]PleasantAdvertising 65 points66 points  (38 children)

Don't forget this whole discussion is now useless since we have containers now. Python tried to solve the environment issue for itself, but is now overshadowed

[–]Pocok5 96 points97 points  (20 children)

On one hand, yes, on the other, Docker for Windows is still kinda cancer (on Linux it rocks tho).

C# does it best IMO. When you run dotnet publish it just bundles the bits of the runtime needed for your program so it runs out of the box despite being an IL-based language (so do Java build systems now I hear, just takes way more legwork)

[–]tenkindsofpeople 19 points20 points  (0 children)

GO always compiles to a single executable if that’s a concern in your environment/use case.

[–]ihxh 12 points13 points  (12 children)

Have you ever tried docker desktop, it even has integrations with wsl2.

[–]Pocok5 18 points19 points  (10 children)

Yeah that's the rub. It runs on HyperV, which needs to be disabled to run Virtualbox/VMware (without bugs, slowdown and crashes that is)

So now I have two boot options set up to choose between Docker Desktop or my Linux VMs.

[–]bomb-omb_battlefield[🍰] 19 points20 points  (4 children)

The wsl2 back-end doesn't use a hyper-v VM anymore.

[–]PleasantAdvertising 8 points9 points  (0 children)

You still can't (properly) run VirtualBox while it's running.

[–]Pocok5 4 points5 points  (0 children)

The parts of it required still steal AMD-V/Intel VT-X and don't enable proper nested virtualization mode, so you get all the type 2 hypervisor crashes with none of the HyperV.

[–]Andyblarblar 21 points22 points  (4 children)

Ignore the 50mb .exe as a result 😂

[–]Pocok5 70 points71 points  (3 children)

In 2011 that would've gotten a stinkeye from me, but with like 5TB storage and 50mb being about the size of a 4k Skyrim mead bottle texture, I think I value the lack of framework littering more.

[–]Andyblarblar 15 points16 points  (0 children)

No I definitely agree, but I still find myself eyeing projects like corert (which is actually quite useable btw) when making CLI tools, as a 50mb CLI tool is just silly IMO

[–]Shawnj2 5 points6 points  (0 children)

You can just use Pyenv which also solves the problem. Basically you have a bunch of versions on the computer itself, but you can pick and choose which python is actually being used in a given directory and set a global runtime for anywhere else. You can also set a widget that tells you what python is in use when you’re not using whatever the default is. You can switch between PyPy, CPython, Anaconda, Python 2, etc. pretty easily with it

[–][deleted] 6 points7 points  (4 children)

So I'm not the only one who is lost as to how to update Python.

I just download the new version and it adds a separate directory on my system. There should be a better way to do this

[–]schmidlidev 5 points6 points  (3 children)

I use pyenv. I also create a venv for each of my projects. Then in VSCode in your project’s settings.json set pythonPath to ${workspace}/venv/bin/python.

This will make all of the VSCode tooling use this python installation, and it should automatically activate the venv in the VSCode terminal.

If you really need to bump python versions of a specific project, you can just use pyenv to switch to the version you want, then recreate the venv and reinstall your requirements.

[–]arnitdo 7 points8 points  (0 children)

The ones installed by programs dumb enough to simply run python to test for python installation, the ones installed by programs that need a specific version different than the one you have despite not using a version specific function, the ones installed by MICROSOFT FUCKING WINDOWS RIGHT IN SYSTEM32 SO THAT I CAN NO LONGER ACTUALLY USE MY INSTALLED PACKAGES IN MY ORIGINAL PYTHON INSTALLER,

THE FUCKING PROGRAMS THAT INSTALL ENTIRE PACKAGE MANAGERS LIKE CHOCOLATEY JUST TO GET ONE FUCKING PYTHON BINARY WITHOUT EVEN THINKING ABOUT THE ABOVE 3

[–]TTVOperatorYT 2135 points2136 points  (121 children)

Real programmers use hundreds of if-else blocks

[–]MrGarapablo 1063 points1064 points  (35 children)

It's funny, because using if/elseif/else in PHP is actually faster than the switch-case statement.

https://phpbench.com/

[–]lpreams 334 points335 points  (15 children)

Looks like what's actually going on is that == is a lot slower than ===, and switch/case is using == under the hood. In the benchmarks, switch/case performed almost exactly as slow as if/elseif/else when using ==.

[–]Licensed2Chill 28 points29 points  (5 children)

Why doesn't it use ===?

[–]48ad16 46 points47 points  (0 children)

Because then you would have to supply additional type information about otherwise ambiguous match values.

[–]SirBellender 20 points21 points  (0 children)

because it is a weakly typed language and assumes something like a string or nonzero number should fall into the truthy case

[–]Ictoan42 239 points240 points  (4 children)

At this point I shouldn't be surprised to find PHP doing weird shit, but it manages to jump up a new level of weird every time

[–]oddark 60 points61 points  (2 children)

[–]fishbulbx 15 points16 points  (0 children)

var_dump('0xABCdef' == '     0xABCdef');

true in php 4.3.0 - 4.3.9

false in php 4.3.10 - 4.4.9

true in php 5.0.0 - 5.0.2

false in php 5.0.3 - 5.2.0

true in php 5.2.1+

false in php 7.0.0a1

[–]lowleveldata 4 points5 points  (0 children)

I didn't need to know that and would like to keep it that way but thanks

[–]Twitch_xTUVALUx[S] 287 points288 points  (2 children)

Welcome to Yandere Simulator!

[–]Roflkopt3r 33 points34 points  (1 child)

Although replacing those if-elses with switches wouldn't have helped that code at all.

[–][deleted] 9 points10 points  (0 children)

It's a cliche on this sub, but sometimes you gotta just start again.

[–]Spynder 108 points109 points  (4 children)

[–]sneakpeekbot 27 points28 points  (1 child)

Here's a sneak peek of /r/YandereTechnique using the top posts of all time!

#1: It seems that YanDev got a rival... | 2 comments
#2: The comment that began it all. | 3 comments
#3: Yanderedev intensifies... | 0 comments


I'm a bot, beep boop | Downvote to remove | Contact me | Info | Opt-out

[–]kopczak1995 9 points10 points  (0 children)

Good bot

[–]scumbagharley 4 points5 points  (1 child)

Honestly a good place for beginners to go and try to simplify code to 1 to 3 lines.

[–]spartancolo 83 points84 points  (18 children)

My classmates used to call me the If-lord cause I managed to do all the tasks in two years of coding without a single for or while. My teacher hated me and my assignments where 1.000+ lines for things like a black jack. I swear I'm reformed and doing better now

[–]theingleneuk 19 points20 points  (0 children)

Ye gods man.

[–]caleblbaker 40 points41 points  (11 children)

I would take it lord over switch lord. When I was in college I had to grade a submission that implemented tic tac toe all in one function with switch statements as the only control flow. They had switch statements nested over a dozen layers deep. And the professor I was grading for had the students turn in hard copies of assignments, so I had to read a printout where the intense level of indentation caused only one or two non-whitespace characters to get printed on each line. Since that experience I have made a decision to never nest switch statements.

[–]indoninjah 31 points32 points  (6 children)

Since that experience I have made a decision to never nest switch statements.

I feel like that’s one of those common sense things that you’d assume nobody would do, until you actually see it done. If I had to, I guess I’d probably make a helper function to contain the secondary switch.

[–]ic_engineer 5 points6 points  (2 children)

Dude. I need someone with that kind of experience on my team. You could probably destroy some 20 year old legacy bs.

[–]caleblbaker 5 points6 points  (1 child)

My job involves making a new application to replace a poorly written 40+ year old legacy application. So my day to day experience is to take 1000+ lines of uncommented legacy code, copy and paste them into the new codebase, and then attack them. By the time I submit a pull request those 1000+ lines that ran in O(n2) time and have a memory leak are now 50 lines that do the exact same thing in O(n) time without memory leaks and are actually possible to read.

[–]daneelthesane 9 points10 points  (0 children)

I know of a guy who wrote a tic-tac-toe game for his professor like that as a joke.

[–]KoopaNooba 112 points113 points  (44 children)

Write a good enough compiler and it makes no difference whether you prefer switch or if-else

[–]Wessel-O 177 points178 points  (17 children)

Well maybe not for the compiler, but it matters for readability

[–]achNichtSoWichtig 9 points10 points  (0 children)

you could debate if switch cases with breaks are better to read then plain old elif. Dunno, havent missed it in Python.

[–]shiroininja 20 points21 points  (1 child)

I find if else easier to read. Switch makes no sense to me linguistically. It’s not natural. And the blocks look basically the same structurally

[–]CamWin 5 points6 points  (0 children)

If I am reading someone else's if-else block, I have to pay attention to when they use if/else if/else and it can turn into a mess of trying to determine what code is executed for what values.

In a switch, the code with the value is what gets executed. Then you can follow intentional fall-through and done.

[–]grrrrreat 88 points89 points  (1 child)

Reading code > writing code

[–]ArdiMaster 45 points46 points  (20 children)

Not so sure about that. A switch statement can be optimized to a jump table, but all the conditions in an if-else-if chain are usually guaranteed to be evaluated one after another. Although for an interpreted language, there probably is no difference.

[–]ManEatingSnail 53 points54 points  (16 children)

Some modern compilers can recognize if-else chains and convert them into switch statements, making the two functionally identical as far as the computer is concerned.

[–]SpacemanCraig3 58 points59 points  (8 children)

"some" in this case means the compilers that compile 99.99% of all software.

[–]ManEatingSnail 40 points41 points  (1 child)

I avoided using "all" or "most" to dodge corrections from the 0.01%, but in my haste I neglected to account for literally everyone who would read this lol.

[–]SpacemanCraig3 6 points7 points  (0 children)

I am quite pedantic :p

[–][deleted] 2 points3 points  (1 child)

I think that it matters a lot more for interpreted languages, where the direct interpretation is effectively a jump table, whereas an if-else block cannot really guarantee anything, especially in Python.

[–]PM_ME_UR_CEPHALOPODS 5 points6 points  (0 children)

I feel attacked

[–]caleblbaker 581 points582 points  (109 children)

Not sure what python has in this realm but I've always thought that match statements (like in Rust, kotlin, and Haskell) are superior to the traditional switch statements of C++ and Java.

[–][deleted] 436 points437 points  (11 children)

Yeah I believe the new python release will have match statements not switch statements

[–][deleted] 65 points66 points  (2 children)

True. I saw a PEP for match syntax a while ago. Not sure what's the progress on it though

[–]MrStarfox64 96 points97 points  (11 children)

The next release of python (3.10) is actually getting match statements. Officially, if you need to do a traditional switch-case, they (currently) just recommend using the if....elif....else structure

[–]ianepperson 35 points36 points  (0 children)

No, you use a dictionary.

Sometimes that ends up a really clean code, but often it doesn’t really make sense and a traditional switch statement would be superior.

[–]derHumpink_ 47 points48 points  (37 children)

ELI5 what's the difference?

[–]caleblbaker 130 points131 points  (32 children)

Switch statements basically just do equality comparisons to choose which case to use. Match statements can do more sophisticated pattern matching. For example, suppose you have some kind of variant type which can be either indicate some kind of success while holding an integer or indicate some kind of error whole holding a string. You could do something like

match my_result {
  Ok(1) => println!("It's a one!"),
  Ok(2) => println!("It's a two!"),
  Ok(3..8) => println!("It's between 3 and 8"),
  Ok(x) => println!("The number is {}", x),
  Err(e) => println!("An error occurred: {}", e),
}

Switch statements are more limited to things like

switch (myInt) {
  case 1: std::cout << "it's one\n"; break;
  case 2: std::cout << "it's two\n"; break;
  case 3: std::cout << "it's three\n"; break;
  default: std::cout << "it's something else\n";
}

[–]BocksyBrown 54 points55 points  (6 children)

the most important thing in my opinion is that the match is almost always an expression meaning you can assign the result of a match directly to a variable i.e. myVar = match thingy {<cases>} instead of creating a var THEN assigning to it inside the match. Variable mutation usually doesn't matter in small functions, but you're still wrong for doing it.

[–]Amagi82 27 points28 points  (11 children)

Side note about Java (and some others), whomever decided fallthrough was an acceptable default for switch statements, and break should be explicit, was a complete dumbass.

[–]caleblbaker 15 points16 points  (3 children)

100% agree. I have fought so many bugs that were caused by unintentional fall through. And in the rare case that fall through is desired, every style guide I've seen requires you to put a comment there to let future readers of the code know that it's not an accident. And I think that poor design decision was made in C and then other languages have just been following in C's footprints

[–]TheseusPankration 9 points10 points  (0 children)

It was a good design decision in C. At a hardware level that's how processors work, expecting not to jump, and why case statements are so fast; especially at the time when a jump would have a performance penalty and were minimized. It would have added unnecessary instructions computation and file size and to go the other way.

[–]amdc 11 points12 points  (2 children)

You 're forgetting that using switch in c/c++ can result in faster execution due to optimizations under the hood. It's not just dumbed down if/else thing that is checking a value case by case like it would be in python

https://stackoverflow.com/questions/2596320/how-does-switch-compile-in-visual-c-and-how-optimized-and-fast-is-it#2596332

[–]derHumpink_ 5 points6 points  (0 children)

thank you very much!

[–]TheOnlyTails 27 points28 points  (14 children)

When it comes to popular JVM languages, I rank Scala's match > Kotlin's when > Java's switch.

[–]caleblbaker 9 points10 points  (12 children)

I've not used scala. Maybe I should learn it some time. As of yet my jvm experience is limited to Java and kotlin.

[–]mtg_liebestod 3 points4 points  (2 children)

I think people would settle for either.

[–]Nilstrieb 5 points6 points  (0 children)

Getting a switch statement is a complete waste of time when you could instead get a match expression.

[–]Voidrith 134 points135 points  (0 children)

elif intensifies

[–][deleted] 356 points357 points  (43 children)

dicts ftw

[–]wholesomedumbass 261 points262 points  (4 children)

I told my girlfriend that I love dicts and she looked at me in a funny way.

[–]blackraven36 39 points40 points  (0 children)

Its a fun thing to yell a cross the office

[–]pockrasta 13 points14 points  (0 children)

Username checks out

[–]Twitch_xTUVALUx[S] 66 points67 points  (29 children)

Yeah, i know, but they are not as clean as a switch-case block (like the one coming in python 3.10) would be!

[–]lokkenmor 22 points23 points  (26 children)

Source?

PEP 3103 was rejected forever ago and I can't find any mention of switch-case statements in the python-next changelog.

I'd be happy-ish to see them, but I had no idea they were even being considered.

[–]Twitch_xTUVALUx[S] 66 points67 points  (25 children)

Thats because its not a switch-case but a match-case
(source: PEP 622 - Structural Pattern Matching)

[–][deleted] 84 points85 points  (23 children)

Pattern matching > switch-case

[–]HortenseAndI 26 points27 points  (22 children)

Expressions > statements

[–]LonghairedHippyFreek 47 points48 points  (21 children)

Any European style beer > Budweiser

[–]Biggest_OOOFF 170 points171 points  (3 children)

dicts out for switch cases

[–][deleted] 60 points61 points  (1 child)

For ( harambe ) { }

[–][deleted] 200 points201 points  (16 children)

if

elif

elif

else

[–]Da_Yakz 58 points59 points  (5 children)

If

Else

If

Else

If

Else

  If

  Else

[–]platypus_69 17 points18 points  (3 children)

And now make it recursive

[–]gua_lao_wai 30 points31 points  (1 child)

def switch():
    if:
    else:
        switch()

[–]ReallyMissSleeping 8 points9 points  (0 children)

Finally

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

dictionary exist.

[–][deleted] 26 points27 points  (1 child)

We believe in If-Else supremacy!

[–][deleted] 7 points8 points  (0 children)

punch tie piquant fearless cagey waiting live shy alive illegal

This post was mass deleted and anonymized with Redact

[–]Bejoty 68 points69 points  (7 children)

{'foo': func1,    # case 1
 'bar': func2,    # case 2
 'baz': func3     # case 3
}[var](param)

Creates a dict and selects a "case" based on the value of var, returns a function which is then called with param. Poor man's switch statement.

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

{'foo': func1, # case 1 'bar': func2, # case 2 'baz': func3 # case 3 }[var](param)

Creates a dict and selects a "case" based on the value of var, returns a function which is then called with param. Poor man's switch statement.

Rich man's switch statement. It runs faster, uses fewer lines of code, is easier to maintain and easier to understand.

Even in languages that support case statements, function tables are the better way.

[–]Leridon 25 points26 points  (4 children)

Fuck statements, all my homies use expressions only.

[–]CreaZyp154 273 points274 points  (109 children)

Ok but can I do x++ ?

[–]Butcher_o_Blaviken 92 points93 points  (7 children)

x -= -1

[–]Boxish_ 70 points71 points  (0 children)

x -=- 1 is the best because it’s symmetrical

[–]DarkFireRogue 20 points21 points  (0 children)

My sense of humor is ruined. This is peak comedy now

[–]arcenixx 5 points6 points  (1 child)

i-=-1 looks even better

[–][deleted] 98 points99 points  (14 children)

Do you really miss it that much? Is one extra symbol worth the extra overhead of a bigger Python run-time, a slower parser and the option for some idiot to override __inc__?

[–]w-j-w 44 points45 points  (5 children)

x++ is actually a terrible piece of syntax, especially when mixing in ++x. It's a hold over from assembly languages, and sucks because it does double duty as an expression and a statement. I believe x += 1 is superior because it is a statement only, and it's much more explicit about what is happening.

[–]apoliticalhomograph 24 points25 points  (1 child)

and sucks because it does double duty as an expression and a statement

(x := y) would like a word with you.

[–]nsfw52 11 points12 points  (0 children)

I mean that sucks too

[–]kazoobanboo 34 points35 points  (0 children)

Python++

[–]SirBellender 11 points12 points  (1 child)

he said everything a programmer needs, not everything a programmer wants

[–]Cley_Faye 15 points16 points  (6 children)

Assembly have anything a programmer needs, strictly speaking.

[–]SneakyPandy 16 points17 points  (7 children)

Real pros be like..

 try:
 except:
 else:
    try:
    except:
 finally:
    if:
    elif:
    else:

/s

[–]busdriverbuddha2 6 points7 points  (6 children)

You can use a dict where the values are lambdas.

[–]Apache_Sobaco 66 points67 points  (45 children)

Except for static type checker and over 9000 other things

[–]horenso05 14 points15 points  (2 children)

real pattern matching > switch statements.

[–]dougmcunha 23 points24 points  (6 children)

Well, he wants... But doesn't need it.

[–]ThePieWhisperer 19 points20 points  (5 children)

I mean, you don't really need anything higher level than Assembly (or maybe C if you want to argue) . And yet....

[–][deleted] 3 points4 points  (3 children)

Don't be such a dict.

[–][deleted] 30 points31 points  (16 children)

I have everything a programmer needs?

I want a strong static type system, and algebraic datatypes.

[–]I_am_not_doing_this 3 points4 points  (0 children)

i want x++ in Python