Ask HN: Why use Python or similar?

36 points by Xelom ↗ HN
Hi,

I am a .Net developer. I love C#, I think it is a great language with fantastic features. I'm looking at new startups job offerings and open source projects source codes a lot. I see a lot of Python or Ruby generally.

I know C# is not open source. That is not the case. You can use Java even Scala. But when I look at performance Python is not near C# or Java. And I can't seem the find features of C# in Python. In my opinion, I feel more comfortable writing C# than Python. What is the reason behind choosing Python if it is slower and lack a lot of features when compared to my preferred language?

I'm not trying to say Python is bad or anything. If a lot of people using it there must be a reason. But I can't see that reason. I'm trying to find it so I can give my time to the langauge.

Thanks

71 comments

[ 2.1 ms ] story [ 154 ms ] thread
If you like C# then use C#.

I think there are some people who see C# as a "Microsoft Thing" and won't have anything to do with it.

As for Python, I don't particularly care for it, but many people find it to be a satisfactory language in the sense that you find C# satisfactory. That is, there are lots of good libraries that you can do to get many tasks done quickly. Performance doesn't matter much in applications that are limited by network latency, disk I/O or waiting for database calls to come back.

I'm writing a lot of little batch programs in my job. Like get those 1M records from db. Process them with an algorithm and write to results to this other table. I think you should write faster with Python. But performance of the processing is also important and LINQ helps a lot with the writing time.
That'll of course depend completely on the nature of the things you do with those records you get from the database, but 1M recors don't seem like much (do you join them with something?).

But if LINQ is what you need, well, Python may have a similar or not, depending on how you use it, but there is no reason for you to start writting C# in Python.

This maybe a bit overkill, but you could take a look at ORM layers. Something like http://www.sqlalchemy.org/ instead of playing with SQL directly.
yeah, sqlalchemy core is pretty good _if you really need to build up dynamic queries_. (I haven't used the full up orm).

But if possible, I still just prefer looking at a full sql statement.

In a lot of cases, the "performance" people care about when making language choice is "developer performance". People choosing Python feel (perhaps incorrectly) that they will be more productive and able to build things faster with that language than any of the other ones they considered.

Contrived example: If it will take you 8 hours to write something in Language A that takes 30 seconds to run, or 30 hours to write it in Language B and have it run in 15 seconds, which do you choose? If raw performance isn't the thing that's valuable to your company, maybe Language A is a better choice (even if it's slower than Language B).

(comment deleted)
Use what works for you.

In my case, that's python.

It's portable (cross platform), the final product is (usually) the script files themselves vs. an architecture specific "executable", and the language includes "batteries" I find useful in the standard distribution: XML, JSON, and (a limited amount of) HTML parsing, URL parsing and retrieving, regular expressions - stuff that makes what I love to do (data wrangling) easy.

Additionally it has the benefit of being pre-installed on my OS of choice, OS X, and in that specific distribution a very useful third-party library comes pre-bundled: pyobjc

This means that I can write what is, in essence, a plain text file that "runs" on most any recent version of OS X and has almost full access to Apple's ObjC (and C) APIs for controlling their systems. Talk about power!

And if someone on OS X wants to change the program? Nothing more than a text editor is needed. No need to re-compile, install an IDE, or a set of developer tools (outside of the python interpreter itself, which is pretty much on everything but Windows out of the box).

This last reason is specifically why python is very popular in the OS X sysadmin world right now. A large number of great open source OS X tools are written in python.

Beyond just OS X, you can use python and most other languages in that space on cheap, ubiquitous hosting without having to pay extra licensing fees. For me, this is one of the biggest advantages to going the open source route. There are, of course, downsides to not having a company backing your tools, but they can be balanced against the community associated with the your language of choice.

There's also the build/test/deployment cycle when developing, which pudquick touched on indirectly. With scripting languages, deploying a new build to test is usually as simple as saving and restarting the process. Sometimes you don't have to go past saving. The last time I dealt with C# and languages in that space (C/Java/ etc..) a build cycle involved a compiling a potentially several more steps. If you've worked with languages that don't require that, having to use one that does can get annoying pretty quickly. Especially if the project is of any size.

Another big point is the availability of libraries to do things you want to do. Most of the open source language platforms have large repositories of libraries that you can pull into your project with ease (pip, rubygems, cpan, pear, clojars, etc ...) and management tools (gem, bundler, cpan, perl-lib, composer, leiningen, pip, etc ...) to simplify installing/using those libraries.

One other thing, and this is one I see as absolutely huge, python, ruby, php, etc encourage pull people into a larger community if you get beyond anything trivial. This exposes a developer to other languages, other platforms, and other ways of looking at software. Of course, that means the developer has to go looking as well.

Still, there are four (non-comprehensive) big reasons to use any language:

* It pays the bills

* Its a good technical choice for the "job"/project

* You like working with it

* You have to maintain someone else's toys

Check for youself, rewrite some code of yours in python ...... my guess it will be half of C# size, then try again and it will be 1/4 and then you stick to Python .... or maybe not :-)
And I can't seem the find features of C# in Python.

Which features of C# are you looking for in Python that you can't find?

Also, keep in mind that something that is written in good, idiomatic C# might need to use different idioms in Python in order to make sense as a Python program (and vice versa).

I'm not the original poster, but one of the things that I can see as much easier in c# than in python is process control. The power of the .net process library [0] seems to be much greater than the power of the python process library [1]. Being able to create events on process behavior, etc is rather useful, and the python tools don't seem to give anywhere near that amount of flexibility.

[0] http://msdn.microsoft.com/en-us/library/system.diagnostics.p...

[1] http://docs.python.org/2/library/os.html

That isn't really the "python process library", most of it is an agglomeration of thin wrappers around standard POSIX interfaces.

The subprocess[0] module is probably closer to what you're looking for, though it still does not operate the way you want it to. It's also possible you may be looking for something that you can get by proper use of dtrace.

More broadly, you're not looking at the C# process library there, either. You're really looking at the Windows process library. It's predicated on a different culture and different assumptions and some of its functionality may be difficult or perhaps even impossible to implement on other systems (I note the Mono implementation has several explicit NotImplemented portions).

[0] http://docs.python.org/2/library/subprocess.html

If you're a C# programmer and you want to stay one, then stay one. There are many startups out there that would love to have a skilled C# programmer, I'm sure.

It might depend on where you're living, I'm not sure.

Also, afaik C# is open source, check out Mono. (somebody correct me if wrong)

Performance is not the only reason to choose a language. Suitability to the particular project, availability of useful libraries, developer productivity, team members' existing skillset, health of the language's community, speed of change in the language, integration of the language with tools already in use, ease of learning for new team members.

In my experience as a sysadmin running hundreds of different applications written in a wide variety of languages over the years, I've seen some of the worst performing apps written in the "fast" languages you mention, and some of the best performing written in Ruby and Python. Of course, it can go the other way as well. My point is that the quality of the code has as much to do with "speed" as the language chosen.

And I could go on and on about maintainability, ease of refactoring, and more. But I think you probably get the point.

If you're curious about why people like Python, the best way to understand it is to learn it yourself. There are lots of free tutorials online and if you are already a skilled OO programmer, you should have no trouble picking it up. Spend a few weeks investigating it, and you might find that you prefer it, too!

Generally speaking, the Python community believes the strength of Python is in its culture, which can be summed up by the Zen of Python: http://www.python.org/dev/peps/pep-0020/. Python also recieves much praise for being easy to learn, readability, productivity, expressiveness, and its library support, namely NumPy and SciPy (Python is often used in scientific computing for this very reason). Aside from this, Python programmers like it and that's reason enough. Performance for Python (and Ruby) is adequate for most use cases.

Personally, I don't like Python. If I want a dynamically typed language, I use Ruby, but I came to this conclusion after spending time with both languages, and it wasn't the feature set that sold me of one over the other, but rather how I felt while working in the language and its ecosystem. In other words, whether or not Python or Ruby are worth your time is a decision you'll only be able to make after using the language.

I believe in being language agnostic whenever possible. Don't hold strong opinions about languages. People invest a lot of time and energy into their tools. This is a "sunk cost." Their time and energy can't be reclaimed. So it's no surprise that, when asked, they will praise their tools -- even if they aren't objectively all that great -- solely to justify the costs. They aren't trying to convince you, they're trying to convince themselves. So don't waste time trying to find feature comparisons. All languages have redeemable values, and learning a new one is always a valuable exercise.

So give Python and Ruby a shot. You might find you actually like them, but not for the reasons you expected to. And if you don't like them, at least you'll know why.

I have to disagree with the last part of your post. Some languages are actually better in quantifiable ways; for example, shorter development time, better performance, etc.
But no language is best in every case. Putting skills into language agnostic pursuits means you can use whatever is objectively best as needed, ideally you'll have already used it.
Python code is smaller, and had better package management. I am sceptical you have something in C# that does not exist within a "pip install XXX" in python.

Python requires less LOCs than C#

Less code to maintain and it's quicker to write. Binding to C is no problem either when you need raw performance.

You can run it on a wide range of systems too.

Actually p/invoke in .net is as easy to use as cffi, so c bindings are not a big differentiator between those languages.
It's easy. Like, really really easy. That alone makes up for a lot of flaws. Also, it's easy in a way that makes it easy to mold to the problem at hand. It's my goto language when I just want to throw something together, and for a few more complicated projects as well.
It is not just language. It is the platform behind language. You choose python/ruby your stack probably is opensource/linux stack. If you choose c# you probably is stuck with windows stack, (in which you don't have a lot options like open source stack).
> probably is stuck with windows stack

You're also stuck with licenses for everything from your development environment to your web server, database and not to mention operating system.

Compared to Python/Ruby where development cost can be $0.

Mono's also free, and works well on unixes, and I've had no problem with components for various FOSS databases. There are some differences between the ms clr and the mono clr, but then again, there are differences between jython and cpython as well.
You are also stuck with the costs of managing those licenses, and the risk of not being in compliance (howerever well you manage them). Both get bigger than the licensing costs when you get big.

There is also the cost hidden in "new developer? Wait a bit while I'll get another license", "what kind of replication? We don't have enough licenses", "no, you can't just create a small project (that can get huge) for solving this small problem, it can't justify the cost in licenses", and other very common situations.

One thing that I really like about Python is the interactive style of development that it encourages, at least for me. I tend to do Python development in emacs with source code in one buffer and a repl in another buffer, building my program up slowly and evaluating bits of it as I go along. This style of development works very well for me. There are other languages where you can do development like this but (and please correct me if I'm wrong), this doesn't seem to be the way that people write code in C#.

Secondly, with Python, the most popular implementation of the language is supported on all the main platforms. Compare this to C# and .Net where, although you can use Mono on Linux and Mac, you'd not be using the same runtime that folks would be using on Windows. Call me paranoid but I still don't wouldn't trust Mono as much as I would trust CPython or Microsoft's .Net runtime.

Having said all that, and despite the fact that I've written little C# code in anger, I still do like the look of C#. Microsoft seem to have done a pretty good job of updating the language over the years to support stuff like generics and functional programming. They've certainly done a better job on this front than Sun/Oracle have done with Java. If C# works better for you than Python does on the platform that you use then I wouldn't worry about sticking with it.

(EDIT - typo)

Smart comment, the very accessible REPL can inject some fun into development.
As some others have pointed out, speed of the application at runtime is not always the most important consideration, else we'd probably be doing everything in a mix of Fortran, C, and Assembly.

I have found that developer productivity is higher, especially when it comes to maintenance. For me, all the tools in an IDE help offset Python or Ruby's simplicity when working in say, Java, when writing new code. But when reasoning about old code I haven't touched in a while, or looking at others' code, Python is easier for me to reason about.

If casting about for a reason to learn any new language, I can think of a few. Understanding another language and how you approach problems with it may provide insight on how to better use your main language. Your career may be long, and your main language may not be popular forever. When acting as a hiring manager, I look for people who know multiple languages, even if I only need one. Especially if they tackled learning them outside of school. It shows both passion and initiative.

    using System;

    public class Hello
    {
       public static void Main()
       {
          Console.WriteLine("Hello, World!");
       }
    }
vs

    print "Hello, World!"
Everything is just simpler in python and ruby.
If I open a new console application in Visual Studio. I have all those boilerplate ready for me so I'm also typing:

Console.WriteLine("Hello, World!");

That's all. So I'm not even talking about the greatness of the C# IDE.

> If I open a new console application in Visual Studio

That's the thing, you need an IDE to be able to skip the boilerplate, whereas both Python and Ruby have a great REPL that is available from virtually every terminal emulator.

I like my IDE. It makes the development really easy. I have intellisense and fantastic debug features. I install it one time and open it every time like how you open your terminal.
I'm just saying that C# comes with a lot of baggage. You may like that baggage, but most people who chose Python/Ruby instead of C# don't.

Personally, I like C# the language. I hate the baggage.

Then you're in Windows.... And Visual Sudio is a best in class IDE, but that still means it's an IDE.

(read: there is just a MASSIVE culture gap. It's just as unfathomable to Ruby/Python people why you'd ever use C#.)

I really don't see any bad point of using and IDE. Or being in Windows... It's making me a lot productive. I don't have to search the docs when I forget a function, it is there with intellisense(and explanation of it). I can debug every piece of my code with the values of my variables. It handles a lot of the boilerplate for me too.
Like the parent of your comment said: There is nothing bad about it. It's a cultural gap.

I would never resort to a language like C#. It doesn't mean I think less of the language. I just prefer Ruby.

I like not having to use IDEs. I like my text editors.

Discussing languages is like discussion flavours. It's just preference. It's like asking me why I prefer bananas over apples. I just do.

As I said, it's culture. C# is a pretty decent language, but all of this is so far removed from what I (and others) want that they sound like negatives, not positives.

In the exact same way that I might say "I use Arch Linux, and the only GUI program is Firefox. I live my entire life in the terminal, in vim, and I use languages that have no ceremony in the first place and prefer log statements to debuggers." That probably sounds like all downside to you.

Use whatever you like. It's not that important.

The difference is that Python and Ruby (and similar languages) don't have this boilerplate to begin with. Someone writing Python for a living doesn't have to use an IDE (most don't), but for a Java dev it's almost a requirement.
You can either write a program that'll help you create programs in a language it can understand (the IDE way), or you can use a language that's so powerfull that no program can completely understand, and use it to write simpler programs, avoiding the need of automating things (the Lisp way). Anyway, you can not have both, but you can be in between.

Python is not at the Lisp extreme, but it's not very far from it.

Boilerplate isn't a good thing, even if it's auto generated there is still overhead
Assuming size of a pretty normal simple program is 10k LOC. Lines required to get an entry point of program, Python: 1 (0,01% of such program), C#: 7 (0,07% of such program).

Worry about the things that matter, the number of lines required to get to an entry point is not one of them. Neither is printing to the console.

And to be fair, if you are doing python seriously you would probably also have an ´if __name__ == "__main__":´ line, and a ´main()´ function called immediately from there to avoid shadowing of module variables. Assuming also you want to use the command line arguments you will have to ´import sys´ and suddenly the lines required are almost exactly equivalent short of the braces in C#.

Don't get me wrong, i absolutely love the conciseness of python but i frequently see this silly example being used which in the big picture doesn't mean anything. List comprehensions on the other hand is an excellent example of where the conciseness really shines. Really, you can do things in 1 line that in other languages takes 30. Although C# also has similar feature through LINQ so that's not a killer feature to convert someone from C# to python in particular.

> Assuming size of a pretty normal simple program is 10k LOC.

This is a pretty big assumption, no pun intended.

You can remove or add one digit if you want, the example is equally silly :)
"Worry about the things that matter, the number of lines required to get to an entry point is not one of them"

Getting started on prototyping ideas is most of my life. I rarely use the code I write.

There is a performance criteria that is fundamental to the use of interpreted languages and justifies their use based on other criteria. It's called "good enough."
To some extent if you choose C#, you also have to choose windows, visual studio, iis, etc. In python you can take each of those pieces and exchange it for something else. A lot of people like to do that.

Fortunately if there's no need for a lot of .net ecosystem, you can run .net on linux and similar systems using mono. (I have and it's a great platform to write for) But as soon as you want something more than the bare .net you'll realise the pieces are hard to find.

Regarding the missing features - I don't think there's anything missing, you just need to know where to look. Only linq may not be available since relies on specific syntax, but due to what you can do dynamically, it's not that needed in the first place.

Python is easy to get something up and running in quickly. That is, I would say, its primary benefit -- faster iterations. It also has a syntax that makes it easy to read and write Python programs, and lots of libraries.
Don't try to evaluate the languages in isolation, because it's the libraries and ecosystems of the languages that make all the difference in your life. If you evaluate Erlang the language in isolation, you might hate it. But if you evaluate it while trying to make a server that will scale up from 1 to a hundred machines with fail over and no single point of failure, you might find yourself adoring it (in which case you should look Elixir). Python has its sweet spots of amazing things too, defined mostly by its easy to use libraries for doing everything from image processing, language processing, face recognition, and you name it.
(comment deleted)
Python has bindings/libraries for everything under the sun.
I've used python for at least 8 years, all on linux. It works fantastically well for complex shell script replacements and small projects like web interfaces. It is extremely expressive (lots of power on a few lines of code) while being readable (at least certainly to english speakers, since strange symbols are minimized).

We replaced an old perl-cgi web site with it, and I wouldn't touch anything related to perl with a 10 foot pole ever again in my life time (cough, cough, ruby). Perl is astounding at hiding bugs.

Python integrates pretty well with Unix kernel apis, and the unchecked exception handling is fantastic IMHO. If you want, you can also do exactly what GO does and have multiple return values (for apis you define), but I haven't felt the need to do that too many times.

I also like pythons string and regex methods. C++ std::string is a freaking pile of crap. (even in c++11, I believe).

Where python annoys me is on a large code base (30K lines+). This is where a very bad contradiction in python's zen appears: staying readable. I should be able to read all the call sites of a function, or method, or references of an attribute, and python makes that impossible compared to what I'm sure you can do in c#. Frankly, I was considering whether using mono/c# would be more appropriate, what is holding me back is clearly microsoft's 'intentions' and I don't like being a second class citizen.

It is also frustrating to have many classes of error appear only at run time, which would have been caught by a statically typed language. Edit: I've used pylint and pyflakes, and currently just use pyflakes regularly, but that's a bare minimum of checking.

So I would be very careful about choosing python on a large project. The problem is, it's just so damn good at doing quick development.

One thing that has not come up in other comments -- how many languages do you speak? From the tone of your comment, it seems like, not many.

If so, you owe it to yourself to diversify your language set. Which way you go does not matter so much as going somewhere. So don't over analyze it, just write some stuff in Python for a couple of months and see how it feels. The learning curve is not steep.

As you do this, don't waste energy trying to write C# code using Python, or giving up because feature X is not there.

As an experienced Python dev who recently started programming primarily in java: You should build something big in a scripting language - probably ruby or python - the different style really will make you a better developer.

I think learning java made me better ad designing classes (I never explicitly used a singleton in python before) and helped a bit in my thinking about concurrency.

For python or ruby try to follow the common idioms rather than programming in a familiar style. I'd recommend avoiding classes altogether at first, trying to get as much done with the built-in data structures and functional aspects of the languages.

For me the thing I miss most in java is the interactive interpreter. I usually develop with an ipython instance open so I can test out different ideas and explore the features of a new package quickly. I believe that is why scripting languages are favored by hackers and startups.

> I never explicitly used a singleton in python before

And that's the right way to do it. Python has global functions, Java needs singletons.

It's simple. Speed and cost.

Python/Ruby projects cost less fixed cost to develop and maintain. They cost more to run and scale.

Startups lack development and maintenance costs and generally scaling dollars are followed by revenue or additional funding to finance that cost.

--

Additionally Python/Ruby have a lot more features, frameworks, extensions that specially deal with modern startup issues.