views:

333

answers:

8

I've got a pretty strong background in C-style languages. And have worked on several different types of projects. I have just started taking a serious look at Python after reading Programming Collective Intelligence. I understand that Python can solve any problem that C# can, and vice-versa. But I am curious to know from those who use both regularly, when they choose one over the other. Removing other factors, like coworkers experience, etc.

When do you choose to create an application in Python instead of a static typed, purely OO language like C# or Java?

Edit:

I was afraid we were going to get off topic a bit with this question. Python is an object oriented language. But, as is stated below it may not be the preferred language when your application will have a very heavy business domain, etc. I am aware that Python uses objects extensively, and that even functions are objects, something that is not true in all of the "OO" languages I brought up earlier.

Despite my poor choice of words in the question (almost no languages fit nicely into two or three word descriptions, and it is really difficult to point out differences in languages without it appearing that you are talking down to a certain class of developer.), I am still very interested in what you have to say about when you prefer Python to other languages.

+3  A: 

I almost exclusively use Python to support my development of software in other languages. I should stress, this is not a result of some failing in Python, rather that the software domains I am working in tend to have other languages/frameworks that are more appropriate or simply the only option:

  • Web Development : I would love to check out Python on Google App Engine, but at the moment I am doing all my personal web development in PHP.
  • Desktop Application Development : I use the Ogre SDK for developing windows screensavers and use C++/Win32 for that.
  • Server Application Development : Writing server side software for Windows professionally is almost always in C++.

However, for all of these application domains I use Python regularly write tools, process data and generally to streamline my development efforts. A few examples here are probably the best way to describe how I tend to use Python:

  • To scape data from existing websites.
  • Generate reports based on XML data.
  • Generate sets of SQL queries to populate databases based on other data formats.
  • Parse entire C++ projects and pull out a distinct set of error messages and their corresponding error codes.
  • Compare data sets to find data that I have inadvertantly lost.
  • Image processing to generate data for other sotware.

Python is such an empowering and useful language that although I have never used it as the primary language for software development, I would like to.

Cannonade
You could use Python with Ogre (PyOgre)
GuiSim
Thanks GuiSim, I clarified my answer.
Cannonade
in re "Web Development:" You don't have to commit to GAE to use python. have a look at django (which is a major part of GAE anyway) and on your own hardware (http://djangoproject.com)
TokenMacGuy
+2  A: 

JavaScript and Python have affected how I program even in C now. I think the best thing about knowing multiple languages is that you get more tools to use mentally, because you often don't have a choice of which language to use.

Nosredna
+10  A: 

My motto is (and has long been) "Python where I can, C++ where I must" (one day I'll find opportunity to actually use Java, C#, &c &C, in a real-world project, but I haven't yet, except for a pilot project in Java 1.1, more tha ten years ago...;-) -- Javascript (with dojo) when code has to run in the client's browser, and SQL when it has to run in the DB server, of course, but C++ and Python are my daily bread on the "normal" servers and clients I develop, and that's the case in all parts of Google I've been working in in 4+ years (there are many parts using Java, too, I just never happened to work there;-). Hmmm, there's pure C when I'm working on the Python core and related extensions, too, of course;-).

Neither Python nor C++ are "strictly OO" -- they're multi-paradigm, and therein lies a good part of their strength in the hands of programmers who are highly skilled at OO and other paradigms, such as functional, generic, declarative, and so forth. I gather C# has pulled in some of each of these too (sometimes surpassing C++, e.g. by offering lambdas), and even Java has had to succumb to some (at least generic) to a tiny extent, so surely it's clear that "one size fits all" doesn't -- multi-paradigm programming is alive and well!-)

C++ (like C) forces me to control all memory carefully (our internal c++ style guide forbids the use of "smart pointers" that amount to poor implementations of garbage collection!-), which multiplies my work a lot, but helps ensure I'm not using one bit of memory more than strictly needed at any time: so, C++ (or C when needed) is the choice when memory is tight and precious. Otherwise, the extremely high productivity of Python (and Ruby or Javascript aren't all that different, if that's what you are used to) makes it preferable.

I'm sure there IS a niche in-between for a language that's garbage collected but mostly static, like Java (or C# before it started piling on more and more features, including dynamic ones in 4.0, I hear), or else those languages and cognate ones wouldn't be so widespread -- I've just never found myself inhabiting that peculiar niche, as yet.

Alex Martelli
I am curious to know what the argument about not using smart pointers in your style guide is. In our style guide it is just the oppossite: "only use raw pointers when you are forced to and add a comment explaining why". With the rationale being that the system will take care of memory where/when it has to.
David Rodríguez - dribeas
Note also that neither Java nor C# are pure OO languages either. In a pure OO language, all operations are performed by sending messages to objects, and Java allows for static methods. You can write procedural code with Java just as you would in C.
David Rodríguez - dribeas
+1 for cool motto.
ilya n.
@dribeas, if you write code in static methods in classes in Java you cannot _point_ to it like you could with function pointers in C and C++; you need an interface with a method, and a reference to it, instead -- seriously handicaps advanced procedural coding. As for static methods, hey, even in Smalltalk you can do just the same with classmethods, or any other methods on a singleton object -- and in Ruby you write functions that are nominally "new methods of Object" but walk and talk _exactly_ like functions; so what's a "pure OO language", then?-)
Alex Martelli
@dribeas, see http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml under "Smart Pointers", though it's more permissive (if grudging;-) on this point than what we actually use internally; the paragraph with "we prefer that we design code with clear object ownership" (what a clumsy phrasing, sorry!) is a concise explanation. reference counted pointers "amount to a poor implementation of garbage collection" (scoped_ptr is fine _if_ you need pointer semantics, as it still asserts ownership clearly and sharply).
Alex Martelli
+1  A: 

Python is much more strictly OO than Java and C#.

But if your question is when to use Python and when Java or C#, I find Python useful for small programs that build on existing libraries and don't involve much domain modelling. For example, little desktop utilities written with the Python Gtk bindings or website maintenance scripts written with lxml and elementtree.

When there is a lot of application domain modelling to do, especially if the domain is poorly understood or changing rapidly, I find Python's limited tooling makes changing the code very arduous compared to Java (not so relevant for C# because .NET tool support trails Java by a few years). So for projects like that I'll use Java and IntelliJ.

Nat
Sounds like you need to look at other python editors (see http://stackoverflow.com/questions/81584/what-ide-to-use-for-python).
Tom Leys
"Limited tooling"? What does that mean?
S.Lott
I've looked at a lot of Python editors/IDEs and they are all a decade or more behind Java IDEs and, well, several decades behind Smalltalk!By "limited tooling" I mean that compared to the state of the art tools for Java the development tools for Python are very primitive. IntelliJ (and Eclipse, which is almost as good) gives you a vast array of program navigation, analysis and transformation tools at your fingertips. When using IntelliJ you think about development mostly as applying program transformations, not about editing text. This lets you make large-scale changes easily and quickly.
Nat
A: 

Both C and python are my languages of choice, but I almost always start doing something in python for correctness, and then dive into C when needed. I am mostly using programming for research/numerical code, where the specifications keep changing, and C is an awful language for prototyping (this is true for most statically typed languages in my experience). When you have something working in C, you rarely change it significantly so that it is 'better', because you don't have the time. But sometimes, C is easier than python when you need to control resources (be it CPU, memory, etc...).

So the question really is "when is python not enough for the task", rather than the contrary.

David Cournapeau
A: 

Generally the language is dictated by the job, who wants the stuff done and who you are working with. I only use java and c/c++ for my programming needs, mainly because the people i work with use it. That being said ive used python for fast prototyping and such.

Tore
+3  A: 

I select Python as often as possible. It is the most useful and productive programming environment that I know of.

If I run into projects where Python cannot be used directly or for the entire project (for instance a .NET-based app. server) my approach is usually to do as much with Python as possible. Depending on the situation that might mean:

  • Embed a python interpreter
  • Use Jython
  • Use IronPython
  • Use some IPC mechanism (usually http or sockets) to call an external python process
  • Export data - process using python - import data
  • Generate code using Python

From my answer to a previous question: I know C#. Will I be more productive with Python?


In my experience, what makes me more productive in Python vs. C#, is:

  • It is a dynamic language. Using a dynamic language often allows you to remove whole architectural layers from your app. Pythons dynamic nature allows you to create reusable high-level abstractions in more natural and flexible (syntax-wise) way than you can in C#.
  • Libraries. The standard libraries and a lot of the open-source libraries provided by the community are of high quality. The range of applications that Python is used for means that the range of libraries is wide.
  • Faster development cycle. No compile step means I can test changes faster. For instance, when developing a web app, the dev server detects changes and reloads the app when the files are saved. Running a unit test from within my editor is just a keystroke away, and executes instantaneously.
  • 'Easy access' to often-used features: lists, list comprehensions, generators, tuples etc.
  • Less verbose syntax. You can create a WSGI-based Python web framework in fewer lines of code than your typical .NET web.config file :-)
  • Good documentation. Good books.


codeape
A: 

All of them.

Except for code that is already written in different language, obviously.

Even if something seems too big for python to handle, I usually make Python prototype anyway, mainly because it's goes so smooth. Often I'll stick with Python eitherway and just use C API or ctypes to tackle bottlenecks (after I rewrite the prototype in a nice clean manner, that is).

kurczak