Ask HN: Why use Python or similar?
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 ] threadI 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.
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.
But if possible, I still just prefer looking at a full sql statement.
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).
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.
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
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).
[0] http://msdn.microsoft.com/en-us/library/system.diagnostics.p...
[1] http://docs.python.org/2/library/os.html
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
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)
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!
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.
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.
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.
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.
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)
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.
Console.WriteLine("Hello, World!");
That's all. So I'm not even talking about the greatness of the C# IDE.
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.
Personally, I like C# the language. I hate the baggage.
(read: there is just a MASSIVE culture gap. It's just as unfathomable to Ruby/Python people why you'd ever use C#.)
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.
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.
Python is not at the Lisp extreme, but it's not very far from it.
Everything is just simpler in HQ9+.
Simpler and easier to read.
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.
This is a pretty big assumption, no pun intended.
Getting started on prototyping ideas is most of my life. I rarely use the code I write.
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.
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.
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.
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.
And that's the right way to do it. Python has global functions, Java needs singletons.
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.