Why Python over any other high level language?

I’m a long time C++ and C# developer, but recently have been asked to contribute to projects that are written in Python. Moving from C# to Python is a bit annoying for me as I’m used to knowing or quickly finding out the type of a variable or what a function returns, or when a variable is first initialized or what level in the class hierarchy it is defined. These things are cumbersome for me while writing in Python. Am I missing something? It feels like a step backwards. Is it used mainly because it is what is being taught in schools? I can’t say it does anything better than C# and some things are just flat out worse.

…and don’t get me started on Python deployment.

It’s really easy to throw together prototypes in Python and the number of libraries is huge.

I also hate that it doesn’t require defining variables and types. I think there are some utilities (lint?) that will look at your code and warn you about typos and such.

Python is easy to learn and in general easy to program. C++ and C# are not. I believe it is that simple.

C++ is an extremely powerful language but nearly none wants to learn it any more.

As a primarily C++ programmer I will.use python for scripts (stuff like “copy all the files in this tree starting with Foo to folder Bar after replacing Bloop with Floop in each”). You can do those in most high level language but python is good at it because so much is included in the language itself that would need to be a third party library in most other other languages

It’s also become the default for a lot of AI work, so if you are doing machine learning then you will find most of the libraries, examples, tutorials are in Python

Python is open source, and pretty intuitive for new programmers. Maybe six or seven years ago a bunch of mid-20s data scientists lobbied me to get it added to corporate systems, rather than learning SAS or R from scratch, because that’s what they’d taught themselves/learned in college. Apparently it’s gotten even more popular there for DS aork since I retired.

I recently started learning Python myself just to see if it can do some things better than R….

You can certainly write something like

def foo() -> float:
  return "hello"

in Python and the interpreter will not complain. I guess if you need rigorous type safety maybe you should indeed use a different language? type(foo()) will then return <class 'str'> though; it won’t lie to you

I think that Python rose to prominence due to the widespread use of open source software. When you’re writing a program that will run on a wide variety of unknown platforms, using a scripting language is very convenient. Compiled languages may have binary dependencies on hardware platforms. As long as the system has Python, the Python scripts will run. And using a scripting language makes it easier for people to make contributions. That means the open source project will have an easier time of getting people to contribute. But a big downside is that there is no compiler to catch syntax or variable type errors. You often find those simple errors at runtime when the code is run. Projects like these are supposed to have automated test suites to fully exercise the code before the change is accepted into the open source repository. The test suite will hopefully catch these types of errors. This works well in a proper Agile development environment since incremental changes and automated tests are part of the process. But if there aren’t automated tests which cover all the lines of code, then simple syntax errors can make it into the final product.

If the question is why the guy wrote Python in the first place, there is a statement in the Python FAQ that he wanted a generally extensible scripting language (some features of which he based on some previous interpreters he had designed/worked on), initially used for system administration scripts. Open-source language, as has already been mentioned.

From my perspective, Python is simply more fun to program in. It doesn’t force you to do all the boring work that other languages want you to (i.e. declaring variables etc.). And as stated, there are a lot of libraries which provide you with a lot of routines that you thereby don’t need to program, while they are very easy to incorporate in your program.
Of course, things may go horribly wrong precisely because of the lack of declarations etc.
If I may use a metaphor: programming in Python is like cooking where someone else has prepared all the stuff, you just have to throw the stuff in the pots and stir. And you don’t have to worry about the mess you make. There is still a mess but you let other people deal with it.

My wife is in data science (with a Ph.D.) When I started dating her, she didn’t like Python, being a bit of a C purist (though she did like Java/C#). Over the years she’s grown to love it, and I haven’t seen her write any C or C-type code in a decade. (It’ll be a cold day in hell before she gives up vim and you make her work in an IDE, though.) Plus, yeah, lots of libraries and she can interface with C when she needs to. I only tinker, and I cannot fathom why anyone would program in C unless you need such low-level control given the modern options, but I guess that’s why I’m not a programmer.

When they say Python, they really mean numpy. Without it, Python is yet another general language, but this adds matrix and statistical improvements.

YMMV, both can do mostly the same things so it might come down to syntax. I find R kind of clunky but others like it, glad they finally figured out that the equals sign exists… Both have parts that are indirectly or rather explicitly influenced by MATLAB (not free), there’s also Octave (free but kind of like the LibreOffice of MATLAB where some things are different and frustrating).

Re: type safety, most languages these days are much more lax than C and have been for decades. Or they have an option to enforce it but it’s not on by default. But really we shouldn’t fight over C++ vs Python when there’s Java to make fun of.

I’m curious about “most”. The way I see it, stronger typing has had a kind of renaissance (e.g. pytype for python, typescript for javascript). The junior devs who piled into python because of the lack of boilerplate - including the type system - became intermediate devs who are all “so that’s what that was for”, and simultaneously the rise of type deduction reduced the boilerplate required for strong typing.

Python does have a lot of libraries to use, but this VB programmer really hates it’s structure.

Good point, I didn’t meant to imply that they’re all dynamic now, but there’s certainly more than 30 years ago. But there’s not a dozen different types of integer in these languages, I don’t think?

Because I was curious, I looked at Stack Overflow’s most recent survey, e.g. loved languages, most popular. There’s indeed some “new” ones that emphasize type - Rust, TS, Go, and a lot that provide it as an option, but not usually by default.

I had a long-time programmer coworker who now works at Apple, so he’s no slouch.

He once told me: “Anything that needs to be coded, I can do it in half the time in Python vs C++”.

I don’t doubt one can get some fresh project running in half the time. Can you maintain it for the next 20 years in less than 5x the time of a fully type-safe language with good support tooling?

I’d argue not, but I’m not nearly at the level to be working for Apple and never was. Presumably they have some valid idea what their long-term cost/benefit tradeoffs are.

Though nobody but nobody ever suggested C++ was the way to go for speedy simple implementation. It’s in a unique class of heavyweight evil all by itself.

Hehehe, I periodically have someone try to convince me to use one IDE or another.

I kind of like how easy it is to look at its code and know what it does kind of intuitively. I really barely had to know anything about Python and I could usually figure out what it was supposed to be doing at first glance when I was exposed to it. I can write trivial stuff in it now, but really it seems like the easiest to read programming language I’ve ever seen.

Simultaneously (and somewhat hilariously) I hate that indentations have meaning in the language. It’s like I didn’t know I liked B&D languages until I saw the right one. I still kind of want to fight it and use my natural coding style developed under languages that weren’t worrying about positional formatting - which simply won’t work in Python. Someone who had to clean up my PHP code once described it as “Written by five different crazy people, but all works really well.” My Perl isn’t any better, and my C was almost certainly a batch of barely functioning crazy town. I actually plan to do some useful work in Python soon, and I hope its B&D nature reins in some of my worst inconsistencies in programming style.

It is not like you have to pick one language for everything. Just because Python may be useful to write a numerical analysis script does not make it automatically suitable for a spacecraft control system.

For me there was a transition period from C++/C#/Java to Python. I would write Python code like C++ code, then realize later that I could re-write it and eliminate 80% of my code by adopting a more Python-centric style.

There is also a lot less emphasis on class hierarchies. With duck-typing an object is a Duck if it behaves like a Duck – regardless of where it sits in the class hierarchy. It’s liberating when you realize how many situations there are where you don’t care what type of object you have – you just care if it supports some particular functionality. It’s similar to how Java interfaces are often used.

Another point is that any non-trivial Python program needs good unit-test coverage. Since the language is loosely-typed, you need to put some extra effort to guarantee the objects that were Ducks stay ducks, etc.

A few people have mentioned type annotations. I’ve started using these to document my function prototypes. They aren’t enforced – you have to run a separate tool to check them.

And here in Silicon Valley… spotted last week: