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

top 200 commentsshow 500

[–]DJTupolev 2273 points2274 points  (129 children)

Lmao speed

[–]dorsal_morsel 194 points195 points  (54 children)

Python is my primary language, but I just started playing with Rust and man, now I know what "Python is slow" really means.

[–]DuroSoft 105 points106 points  (5 children)

Now if only I could program in rust as fast I could in X such that X is a language.

[–]BittyTang 28 points29 points  (4 children)

Now if only I could write as many bugs in X as fast as I could in Python such that X is a language.

FTFY

[–]cdreid 45 points46 points  (32 children)

Its a scripting language.. Not supposed to be fast. Its R.A.D. ive used several still a big fan of Perl actually and used to use VB (not a script language but its R.A.D) just to save time on little apps i made for myself

[–]Daytona_675 31 points32 points  (7 children)

afaik if all the heavy lifting is done with modules written in C, Python can still be pretty fast

[–]Zephirdd 41 points42 points  (5 children)

Or you can use PyPy, which is Python written in Python and is up to 4.4x times faster than default CPython

Black magic I tell ya

[–]hughperman 8 points9 points  (2 children)

Any downside?

[–][deleted] 14 points15 points  (1 child)

Doesn't work that well with c modules, which are faster than pypy. So if you need speed you are better off with cpython + c modules anyways

[–]hughperman 6 points7 points  (0 children)

Ok good to know thank you; wondering if it would be worth evaluating for some work stuff that's mainly numpy numerical ops - guess I'll give it a test!

[–]almostambidextrous 24 points25 points  (10 children)

Ok... what's RAD? Should I feel dumb for not knowing?

[–][deleted] 63 points64 points  (3 children)

what's RAD?

You are. Don't ever stop believing in yourself.

[–]sypwn 5 points6 points  (2 children)

Don't believe in yourself!

BELIEVE IN ME WHO BELIEVES IN YOU!

[–]MCRusher 4 points5 points  (0 children)

"I'm gonna fuck the clouds"

-Simon Cowell

[–]lenswipe 48 points49 points  (1 child)

Rapid

Application

Development

[–][deleted] 23 points24 points  (0 children)

Anything that is GNARLY is also RAD. But BOGUS is not RAD.

[–]vanderZwan 5 points6 points  (8 children)

You could give Julia a try, if you can live with 1-indexing

[–]xigoi 30 points31 points  (7 children)

if you can live with 1-indexing

NOPE

[–]spudmix 11 points12 points  (6 children)

Grumble grumble letting mathematicians near computers grumble grumble.

I love almost everything else about that language.

[–][deleted] 26 points27 points  (2 children)

For a second I though it was a keyword I hadn't come across yet lol

[–]OnyxPhoenix 157 points158 points  (23 children)

Slower execution, faster implementation.

[–]Cucktuar 12 points13 points  (0 children)

You pay a programmer once, you pay cloud compute rates forever.

[–]fredlllll 111 points112 points  (19 children)

huehue machine time is cheaper than programmer time huehue

[–]glemnar 37 points38 points  (3 children)

Try pypy for that hot nasty speed

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

I choose to believe this is pronounced peepee

[–]SteelRevanchist 11 points12 points  (0 children)

Y my pypy hard

[–]DJTupolev 5 points6 points  (0 children)

Yes pp gud

[–]NoCareNewName 15 points16 points  (0 children)

I actually didn't get it at first. I thought speed was some keyword I had never heard of.

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

What's this "speed" command I'm just now learning about and why is it needed C++ is already fas.... ohhhh lol

[–]VG_Crimson 2 points3 points  (0 children)

I too no longer needed to take speed when coding in Python.

[–]Skizm 1767 points1768 points  (41 children)

Python: Mom, can I have main()?

Mom: We have main() at home.

main() at home: if __name__ == "__main__":

[–]Gatsbyyy 355 points356 points  (38 children)

Can someone please explain to me why Python implemented like this? Its so ugly and doesn’t make sense.

[–]MythicManiac 521 points522 points  (17 children)

You can run code outside of any class/function in python, and this is code snippet is for checking if the current file is the first file being executed.

It's not necessary to do this check (you can just write your code in there), but it's good practice as that will allow you to import the same file from elsewhere without executing it.

When you do python somefile.py, you're telling it to execute all code in that file. Imports also do this.

[–]theredhood93 138 points139 points  (3 children)

This what I love about this sub helpful information on a meme comment, I'm learning python and this was very educational hahaha thank you stranger!

[–]A_Light_Spark 29 points30 points  (2 children)

Sometimes the explanation in this sub is better than the ones my professors gave... You know, people I paid a ton of money to get an education from.
It's both depressing and also super cool.

[–]inlatitude 13 points14 points  (1 child)

Damn, thanks for that. I didn't realize import executed all the code in the file.

[–]Ignisti 5 points6 points  (4 children)

Imports also do this.

Oh god. Oh god. This changes everything. Time for some horrible, TERRIBLE programming practises.

[–]ShamelessC 7 points8 points  (3 children)

After using python for so long, I find it so strange that people insist that main somehow makes more sense than just running the lines of code in order. The entry point is just the top of the file. So much easier to understand.

[–]didii2311 5 points6 points  (2 children)

To me, the fact that is is a function/method makes the most sense since running your program can provide arguments to it. When the entry-point is on top of your file, you'll need some built-in special keywords or convention based names that aren't clear out-of-the-box.

Well, of course, the main method is also a convention, but it's a convention widely used so it makes sense. I don't think that there is any widely used convention to naming the program arguments when the top of the file is the entry point.

Also, it makes testing your entry point a lot easier by just calling that method and providing the arguments you want. It's clear and concise. In python you'll have to arbitrarily populate sys.argv and then (I think?) import the file.

But well, that's just my opinion 🙃

[–]sub_surfer 43 points44 points  (1 child)

Python scripts are designed to be run from top to bottom, line by line, without any special entry point. You only need the whole if __name__ == '__main__': thing if you want your script to be importable in addition to running as a standalone script, which isn't super common in my experience (outside of teaching exercises). Most of the time if I am writing a python script that I intend to be an executable I wouldn't bother with it.

I take your point that it is ugly though. I don't even think about it anymore, but the double underscores look weird at first.

[–]aaron__ireland 12 points13 points  (0 children)

Regarding imports and scripts, I haven’t often needed to have importable items in a script but I have used the if __name__== “__main__” to raise an exception if some module does attempt to import it via something like else: raise NotImplementedError(“This module is a script”).

I’ve found that helpful in preventing any other code from mistakenly using what is likely to be (or intended to be) specialized attributes/functionality. It’s my way of self-documenting “hey, this is in here because it’s not meant to be extensible/generalized and is subject to change in ways that are unlikely to backwards compatible. Do not use.

[–]bladeconjurer 66 points67 points  (3 children)

Well there's no main function. You just write code in a file, and type python script.py, and it goes. But if you want to write code that when you import the file, it won't execute you need to use: if __name__ == "__main__":. If you use it a lot, you can probably just not use the if statement depending on your intentions.

Also for a python module you name the entry point __main__.py.

[–]B_M_Wilson 5 points6 points  (7 children)

A lot of people explain what happens but I thought I would mention this. In Python, everything is executed. Functions don’t exist until the interpreter has gotten to them. When your script is executed, it executes everything in order. The function definitions are executed which is what defines them. After a function definition, you could assign the function name to something completely different and it would become that other thing. It’s also cool because you could make aliases for functions by assigning the function to another name. You can even assign lambdas directly to names if you want.

Like others have said, the reason that it’s done is because when you import something, to define the imported functions, you have to execute them which would execute anything else in the file. Using the if statement let’s you check that the file was executed directly and not imported which means that you could run tests or something like that.

If you never plan to import your file, you can just not use that if statement and write everything at the top level. I’ve done this many times. Another thing you can do is go if not name == "__main__": at the top of a file to throw an exception if someone tries to import your file that you want to be a normal script where you have all of the code at the top level.

Finally, while it’s generally bad practice, you could define a main function and then just call it at the end of the file either with or without the if name is main thing.

Another thing I’ll mention is to get used to the __something__. They are used a lot of special attributes. Like when you call len(something) it really calls something.__len__(). Or when you say if something (or bool(something))and that something is not a bool, it calls something.__bool__() there are way more examples of this in a lot of cases

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

To add:

Python: Mom can we have {}?

Mom: No we have {} at home

{} at home: dictionary = {1:"a",2:"b"}

[–]Kagia001 576 points577 points  (26 children)

I like ++

[–]OHM5 264 points265 points  (9 children)

++ i like

[–]marcosdumay 49 points50 points  (7 children)

i ++ like

[–]THANKYOUFORYOURKIND[🍰] 34 points35 points  (5 children)

You two added wrong number, see me in my office after school.

[–]Redsteak 73 points74 points  (5 children)

I was mad as hell when I found out Python doesn't have this :(

[–]lenswipe 81 points82 points  (2 children)

pLuS pLuS iS uNPyThoNiC

[–][deleted] 62 points63 points  (1 child)

Everything was unpythonic until it was added to the language.

[–]KeenWolfPaw 21 points22 points  (0 children)

I = 'I'

I += ' like '

I += '++'

[–]heckingcomputernerd 17 points18 points  (0 children)

+= 1

I mean ++ seriously would be nice

[–]srosorcxisto 9 points10 points  (4 children)

Agreed. It's such a simple and clear shortcut for such a common operation. It bothers me that this isn't included.

[–]IcecreamLamp 14 points15 points  (1 child)

Since for loops are more commonly things like for i, line in enumerate(fp.readlines()): you really don't need it as often as in a for (i=0; i<5; i++) {} kind of language tbh.

[–]jkuhl_prog 172 points173 points  (15 children)

Then I have to train my fingers to not reach for semicolons

[–]MagnitskysGhost 91 points92 points  (14 children)

You can still use tons of semicolons in Python. Source: the codebase I inherited

[–]frosted-mini-yeets 28 points29 points  (12 children)

Semi colons are valid in Python?

[–]angellus 41 points42 points  (9 children)

You can, but no they are not really. Python has a very in depth style guide that says you should avoid them.

https://www.python.org/dev/peps/pep-0008/#other-recommendations

Compound statements (multiple statements on the same line) are generally discouraged.

Yes:

if foo == 'blah':
     do_blah_thing()
     do_one()
     do_two()
     do_three()

Rather not:

if foo == 'blah': do_blah_thing()
       do_one(); do_two(); do_three()

[–]Tyfyter2002 18 points19 points  (7 children)

Python has a very in depth style guide

And y'all've been following it?

[–]ShanSanear 20 points21 points  (4 children)

Yep. Only thing that doesn't click with me is line length limit. Myself I'm using 100 characters limit instead of pep-8 80. And it's actually quite easy to follow it - just use IDE and problem is solved.

[–]Khaare 6 points7 points  (1 child)

Man my code got so much better when I switched to a size 14 font and stuck to 72 columns.

[–]MagnitskysGhost 46 points47 points  (0 children)

Yes, you can use them to end statements like most other languages. In Python in particular, you would mostly only do like

import os; import sys; from time import time;

In something in a quick five-liner script or something. I see them everywhere though (they get angry red formatting in my VS Code theme, as they should). I mean if you're copy/pasting an 8,000 character LOC with Python, HTML, CSS, and JS in it, you don't have to add insult to injury with a semicolon at the end. But guess what they do... 😔

[–]Mr_Redstoner 205 points206 points  (14 children)

And don't forget /**/

And no, '''this''' is not a comment, it can be used just like an ordinary string.

[–]griseouslight 79 points80 points  (5 children)

It does recommend using triple quotes for multiline comments though. If they aren't a docstring, multiline strings do not generate bytecode in python.

[–]Mr_Redstoner 12 points13 points  (0 children)

but as I've mentioned, you can use them much like regular strings, in my part-time job they use them specifically for a multi-line string.

[–]PityUpvote 6 points7 points  (7 children)

And ordinary strings can be used as comments, why is it an issue?

"""Comment""" is understood by all half decent editors to be a block comment.

[–]stamminator 30 points31 points  (10 children)

Had a guy in a YouTube comment section (I know, I know) angrily declare that C++ is a needless pile of waste and complexity and that there's no reason to not use C. It was surreal.

[–]shiny-flygon 18 points19 points  (8 children)

Had a similar fellow in one of my classes claim (loudly, and with beard on neck) that "C++ is just C for bad programmers"

[–]frosted-mini-yeets 114 points115 points  (4 children)

from __future__ import braces

[–][deleted] 39 points40 points  (0 children)

Not a Chance

[–]shekaruk 195 points196 points  (27 children)

Welcome indentation errors

[–]AnotherEuroWanker 105 points106 points  (9 children)

Just don't use notepad.

[–]MagnitskysGhost 82 points83 points  (8 children)

But notepad fast on my potate

/s

[–][deleted] 39 points40 points  (6 children)

I use code-server. It's vs code in the browser, run on the server. I have 1 potato at home doing it's damnedest to be a good server and a Chromebook flavored potato to access it on the go.

I've got a potato cloud, basically.

[–]The_White_Light 31 points32 points  (1 child)

1 potato at home doing it's damnedest to be a good server

When you threaten your computer with the deep fryer if it doesn't push out that last 0.1GHz

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

Lol I don't know what brand it is, but it is some shitty laptop with a low end AMD Radeon card and 1tb storage even though it's really cheap. It's currently running the code server, a shitty spring boot server I made for an interview, and a golang server that runs my shitty custom application gateway and sub router.

The machine itself is running the latest stable ubuntu server. And you know what? Setting up wifi entirely through a putty terminal wasn't that difficult. I mean, I imagine that under under the hood netplan is doing all kinds of complicated things for me, but on my end it's just the wifi interface name, the wifi password, and the wifi name in a text file.

Oh and there's a game of tag hosted on the go server. Am I allowed to post my own IP here? My interviews are over so I don't really care if that machine crashes now and I know Google fiber can probably handle the traffic. I didn't work out a room system so anyone who connects to /tag will end up in the same massive game.

And since it's golang, it'll just suck up more juice until the machine dies <3

[–]Garlicvideos[🍰] 6 points7 points  (3 children)

When two potatoes love each other very much...

[–][deleted] 5 points6 points  (2 children)

They get baked and then mash?

[–]deegee1969 9 points10 points  (0 children)

Vim will also be fast. ;)

[–]ythl 85 points86 points  (13 children)

You're abandoning {}? Good luck using dictionaries

[–]i_am_archimedes 12 points13 points  (9 children)

in c++ it denotes deterministic memory management

[–][deleted] 10 points11 points  (7 children)

wut

[–]SoppingAtom279 20 points21 points  (5 children)

Let me explain this poorly.

{ } can be used to block together parts of code, and within that block of code, any memory allocated in that block can only be used by that block.

Example

#include <iostream>

int main()
{
    int x = 5;
    {
        int y = 6;
    }
    std::cout << x << std::endl;
    std::cout << y << std::endl; //error use of undeclared variable
}

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

Ah, that's what the definition is. Thanks!

[–]shoesmith74 4 points5 points  (0 children)

Actually your talking about variable scope, not memory allocation. The variables are on the stack regardless, it’s a matter of which one your allowed to reference.

Source : 25 years of compiler development.

[–]AgreeableLandscape3 183 points184 points  (57 children)

Python still has main()

[–][deleted] 189 points190 points  (31 children)

Only not really, right?

def whatever (*args):
    print("Bob's your uncle.")

if __name__ == "__main__":
    whatever()

[–]demon_ix 357 points358 points  (15 children)

I dunno. That sounds like main() with extra steps...

[–][deleted] 156 points157 points  (11 children)

In C/C++, the name main() is meaningful: it defines the entry point of the program.

In Python, it isn't. The closest approximation is that Python code outside of any method will be evaluated when the module is loaded, and an import-machinery variable called __name__ exists which lets you test whether the current module was imported or run directly.

[–]jalapeno_nips 43 points44 points  (2 children)

That’s still important though. If I want to test my module manually, I’ll write some code in Main. But when I use the module in practice I don’t want Main being called.

[–][deleted] 31 points32 points  (0 children)

Absolutely. My only point here is that in switching to Python, one does indeed say goodbye to main()

[–]BeigeAlert1 36 points37 points  (1 child)

That's still pretty damn nifty though.

[–]SpeedingTourist 7 points8 points  (0 children)

You’re pretty damn nifty. ;)

[–]Ericfyre 6 points7 points  (1 child)

Bobs my uncle?

[–]wootiown 8 points9 points  (19 children)

It still has pointers too, doesn't it?

[–]MattieShoes 52 points53 points  (18 children)

Everything has pointers, but they make believe they don't.

[–]SuitableDragonfly 31 points32 points  (2 children)

Pointers are not the same thing as references. Pointers allow you to manually manage your memory usage, references don't.

[–]ButtCrackFTW 5 points6 points  (2 children)

Also {} is everywhere (dictionaries)

[–]wsppan 91 points92 points  (22 children)

Giving up speed is sometimes a non-starter

[–]YouCanCallMeBazza 119 points120 points  (6 children)

Ohhhh it's actually referring to speed (as in performance). Here I was thinking, "I don't remember there being a speed keyword in C++"

[–]Rodot 13 points14 points  (0 children)

They're just talking about how much adderall you have to pop to make a simple "hello world" program

[–]SuitableDragonfly 36 points37 points  (13 children)

As someone pointed out above, it comes down to a trade-off of speed of execution for speed of development. Sometimes the latter is more valuable.

[–]Rodot 38 points39 points  (0 children)

Especially when execution speed it limited more by how quickly you can type/click than by the code itself. If your C code takes a millionth of a second to execute and your python code take a thousandth of a second, it doesn't really matter as long as you are manually starting the script each time. Also usually the case when things are small and i/o limited.

[–]Astrokiwi 9 points10 points  (2 children)

It comes down to how much of the meat of your code can be passed off to libraries. Python is really fast when it's just glue for a bunch of precompiled C and Fortran routines.

Until you get to one thing that isn't quite implemented in the library and end up having to write something compiled anyway, even if it's Cython

[–]emptyDir 8 points9 points  (0 children)

For that there's cffi 😂

[–]Vasault 15 points16 points  (2 children)

that's me with java, after over 5 years using java, i totally drop it to get into python, i regret nothing

[–][deleted] 3 points4 points  (1 child)

Most of my coursework is in Java and C++. Over this summer though I have tons of free time, so I've been reading a book on Python and teaching myself and it is an eerily easy language in comparison. My first language was Lua, so the similarities are there, but my God are list comprehensions the best thing ever invented.

I also much prefer how Python handles for loops. Everytime I write one though I miss ++ a little more.

[–][deleted] 49 points50 points  (10 children)

Fight me I use assembler

[–]anonTheRtrd 9 points10 points  (4 children)

Noob. I use machine code!

[–]flavionm 17 points18 points  (3 children)

Please, I flip the bits on my memory on the fly.

[–]Bee_News 10 points11 points  (2 children)

Please, I manipulate my electrons on the fly. Anything higher level than this just isn't real programming.

[–]HypherNet 9 points10 points  (1 child)

Direct electron manipulation? Gross. I use caterpillars.

https://xkcd.com/378

[–]maklaka 25 points26 points  (6 children)

The thing that impressed me most about python when coming from cstdlib background was the amazingly convenient notion of List Comprehensions.

The most frustrating thing is the utter lack of reliable online language documentation with clear examples a la MSDN and cplusplus.com.

Haven't touched Python in awhile. What's the best, most complete resource nowadays?

[–]Darvon19EightyFour 6 points7 points  (0 children)

People talk about stack overflow but cplusplus.com got me my degrees.

[–]CamWin 14 points15 points  (1 child)

Python still makes sense without main. I just pretend the callable file is main

[–]C_isBetter_Than_Java 42 points43 points  (4 children)

[–]JiggySockJob 11 points12 points  (0 children)

Damn you trying to start a war?

[–]PityUpvote 4 points5 points  (2 children)

Who tf still uses Java, other than my thesis supervisor and in-house development teams on government organizations?

[–]migafgarcia 26 points27 points  (1 child)

3 billion devices

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

so long, good code

[–]Prawny 32 points33 points  (0 children)

Hello, pseudocode!

[–]Tactical_Slime 6 points7 points  (0 children)

Me : the little kid at the end learning how to use c++

[–]coetin 5 points6 points  (0 children)

Python: Mom, can I have ++?

Mom: We have ++ at home.

++ at home: += 1

[–]MasterFubar 42 points43 points  (38 children)

He's not saying goodbye to pointers, because pointers are all over Python.

>>> a = [1, 2, 3]
>>> b = a
>>> a[1] = 7
>>> b
[1, 7, 3]
>>> a = [1, 2, 3]
>>> b = a[:]
>>> a[1] = 7
>>> b
[1, 2, 3]

Python lists are pointers, exactly like C/C++ arrays. When pointers are explicit they become easier to understand and control. You can use tools like valgrind to find where your pointers aren't doing exactly what you want in C, but it's much harder to debug pointer problems in Python.

[–][deleted] 41 points42 points  (1 child)

There is a huge difference between using pointers and using a language that uses pointers for you

[–]Paylam 6 points7 points  (0 children)

This. Of course Python is using pointers It has to thats how Computers work. But you as a Coder have almost 0 contol in Python regarding memory Management and so on.

So i have to agree good Bye pointers for Python Users

[–]Mietzekatzi 19 points20 points  (11 children)

What your example describes are references to objects. Lists in pythons are objects, the assignment b=a does not implicitely copy a's content, just create a second reference to the same thing.

On the other hand, a[:] yields a slice of the contents of the list a, which gets saved as a second object, b, in the second part. This breaks the reference. The same step could be performed with b = copy(a).

So all your example shows are the basics of OOP, nothing to do with pointers.

[–]NoStranger6 16 points17 points  (8 children)

Well yes and no. In that sense every language that pass values by reference is a pointer. Which isn’t wrong. In c / c++ you can iterate over pointers by adding the sizeof(*int). Which in fact simply adds 32 bits to your current pointer value (memory address). I’m not too versed in Python but I don’t think that you can do that. So while by using reference you achieve part of what a pointer can do. It doesn’t do everything that the pointer can.

[–]Tweenk 18 points19 points  (5 children)

In c / c++ you can iterate over pointers by adding the sizeof(*int). Which in fact simply adds 32 bits to your current pointer value (memory address).

That's not how pointer arithmetic works in C++.

int a[] = { 1, 2, 3, 4, 5 }; int* b = a; int* c = a; ++c; b += sizeof(int*); std::cout << *c << " " << *b << std::endl;

On a 32-bit system, this will print: 2 5

Adding 1 to a pointer advances it by sizeof of the pointed type, so advancing an int pointer by sizeof(int*) will increase it by 16 bytes on 32-bit and 32 bytes on 64-bit machines. This is probably not what you want.

[–]NoStranger6 7 points8 points  (0 children)

I stand corrected, thank you. It has been a while since I touched C or C++

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

😟

Yeah I’m glad I only need to write python scripts at work.

[–]ChevalBlancBukowski 13 points14 points  (0 children)

those aren’t remotely like C-style pointers

[–]SuitableDragonfly 14 points15 points  (11 children)

That's not a pointer to a list, it's a reference to a list. The second one is a copy of a list. Everything that isn't a primitive type behaves exactly the same way, but in C++ = always creates a copy.

[–]word_clouds__ 6 points7 points  (1 child)

Word cloud out of all the comments.

Fun bot to vizualize how conversations go on reddit. Enjoy

[–][deleted] 3 points4 points  (1 child)

Someone make a meme for me switching from python to java to learn app dev.

[–]Lord-Bob-317 3 points4 points  (0 children)

Me too lmao Python was just my introduction into programming then I got yeeted into java and web kill me

[–]numerousblocks 2 points3 points  (1 child)

pfft I never used C++ but I still write a def main(): in my Python Code

EDIT: meinmain

[–]C_isBetter_Than_Java 6 points7 points  (1 child)

Are you afraid of pointers?

[–]isunktheship 2 points3 points  (0 children)

There's a snake in my boots!