r/learnpython • • Jun 28 '20

Can anyone explain why we use Python

I keep reading people say things like ‘because Python is built on C’ and ‘Python is much slower than C++ or Go’, ‘Thats why those languages are used to make most popular games’. I also see people refer to Python as a ‘machine’ language, etc etc. So... why use python then if you can do things faster with another language? And why is Python so popular if this is the case? Seems like python is easier to understand for me at least and much easier to read, but is there some other benefit I’m not understanding? Sorry if this had been asked before, I can’t find any direct comparison information between Python and other languages.

EDIT: Thanks everyone! I feel a much better understanding of why python is fast in a different way from other languages, saving programming time rather than computing time. Definitely got the answers I was looking for.

Another edit: Great insights all around. Thanks to everyone taking the time to answer! Looks like I need to do some more research on Cython as I see a few people mentioning it for when speed is required.

336 Upvotes

120 comments sorted by

View all comments

2

u/[deleted] Jun 29 '20

Would you save time if you program in Python and then translate that to C? (automatically with manual adjustments)

2

u/Losupa Jun 29 '20

I dont know about automatically translating it to C, but yes you will most likely save time if you write it in python while prototyping and are constantly adding new features and rewriting it, then translate it to C if you find yourself needing more speed eventually. ofc it may be better to just write it in C, or at least certain core functionalities that are time sensitive, so you wouldnt have to inevitably rewrite them.

2

u/ErinMyLungs Jun 29 '20

I haven't done this but you can optimize the slow parts of your code into c and leave the rest as python which I think gets the best of both worlds. Fast prototyping and easy to read and write but you only optimize what's actually slow.

Frequently you'll have one main bottleneck that optimizing will give you immense improvements and the rest is generally inconsequential. Or worse you have a bottleneck that can't be worked around (uploading a large file is limited by internet speed no matter how ruthlessly efficient your code is) so once you're under that it's pretty much stuck.

Tensorflow is a good example of using c and python to do high performance work with easier coding via python.

2

u/[deleted] Jun 29 '20

Some libraries already do that namely Pandas and Numpy (and OpenCV to C++ IIRC) and probably many more.

1

u/PricePadraic Jun 29 '20

Curious about this answer also. I see a lot of references to using Cython when necessary. Maybe I need to learn more about Cython to ask a better question; but I wonder if this is the ultimate use of Python why I don’t hear/see more about it. Pardon my ignorance

1

u/[deleted] Jun 29 '20

I think it’s because it’s just used when needed. A lot of the time python doesn’t need a lot of pure performance to get work done.

In places where it does, people use it. It’s a tool like anything else. I’ve rarely needed python to be fast enough to refactor and build a lot of stuff in C, but there are good implementations and you may use them without even knowing it.

Here’s an example:

https://pypi.org/project/ssh2-python/