views:

947

answers:

13

For someone who’s been happily programming in C# for quite some time now and planning to learn a new language I find the Python community more closely knit than many others.

Personally dynamic typing puts me off, but I am fascinated by the way the Python community rallies around it. There are a lot of other things I expect I would miss in Python (LINQ, expression trees, etc.)

What are the good things about Python that developers love? Stuff that’ll excite me more than C#.

+11  A: 

Being able to type in some code and get the result back immediately.

(Disclaimer: I use both C# and Python regularly, and I think both have their good and bad points.)

Mark Byers
Thanks for the comment. I'm looking at picking up another language. Python is at the top of the list. I appreciate your comment.
Chuck Conway
I wish I could +2 this answer.
Jed Smith
It might not be as powerful, but Visual Studio's immediate window does that on simple expressions.
Trillian
note there is the mono repl(which I think can be used with .net)
Roman A. Taycher
+16  A: 

For me its the flexibility and elegance, but there are a handful of things I wish could be pulled in from other languages though (better threading, more robust expressions).

In typical I can write a little bit of code in python and do a lot more than the same amount of lines in many other languages. Also, in python code form is of utmost importance and the syntax lends its self to highly readable, clean looking code. That of course helps out with maintenance.

I love having a command line interpreter that I can quickly prototype an algorithm in rather than having to start up a new project, code, compile, test, repeat. Not to mention the fact I can use it to help me automate my server maintenance as well (I double as a SA for my company).

The last thing that comes to mind immediately is the vast amounts of libraries. There are a lot of things already solved out there, the built-in library has a lot to offer, and the third party ones are many times very good (not always though).

Bryan McLemore
+1  A: 

I like all stuff with [] and {}. Selectors like this [-1:1]. Possibility to write less code, but more something meaningfull, that gives to write Models and other declarative things very DRY.

dynback.com
+8  A: 

I'm primarily .NET developer and using Python for me personal projects.

What are the good things about python that developers love?

I can say for myself - Python is like a breath of fresh air.

1) It's simple to learn, took about a week for me in the evenings. I'm saying about Python + Django. Python syntax is quite simple.

2) It's simple to use. No troubles installing Python + Django on Windows at all.

3) It can be run on Windows and UNIX.

4) I need it for web, so I get cheaper hosting than ASP.NET.

5) All the advantages of Python language over C#. Like tuples - so useful!

The only thing I don't like is that my favorite IDE Visual Studio doesn't support it (I know about IronPython, don't you worry).

Vitaly
+1 for the tuples. How I miss them sometimes...
nikie
+6  A: 

Your question is kind of like a plumber asking why carpenters are always going on and on about hammers. After all the plumber doesn't have a hammer and has never missed it. Python (even IronPython) and C# target different types of developers and different types of programs. I am very comfortable in Python and enjoy the freedom to focus on the business rules without being distracted by the syntax requirements of the language. On the other hand I have written some fairly substantial code in C# and would be very concerned about the lack of type safety had I taken on the same task in Python. This is not to say that Python is a "toy" language. You can (and people have) write a complete medium or large application in Python. You have the freedom of dynamic typing, but you also have the responsibility to keep it all straight (frameworks help here). Similarly you can write a small application in C#, but you will bring along some overhead you do not likely need.

So if the problem is a nail use a hammer, if the problem is a screw use a screw driver. In other words spend some time to learn Python, get to know it's streangths (text processing, quick coding cycles, simple clean code, etc) and then when you are looking at tackling a new problem ask whether you would be better off in Python or C#. One thing is certain. So long as C# is the only programming language you know, it is the only one you will ever use.

Pat O

Pat O
IMHO the best answer here, far better than anything else anyone has said
RCIX
+3  A: 

My language of choice is C#, and I didn't quite see the point for me to learn Python so far. This talk from PDC09 really piqued my interest: the guy demonstrates how you can use IronPython (or IronRuby) to make a C# app scriptable (in his demo, drop a Python script in a text box, and it works with/extends your C# code). I found this really fascinating: I don't even know where I would start to do something similar in C#, and this made me at least appreciate that it brings something different to the table, which could really enrich what I can develop!

Mathias
C# is an interpreted language - you can easily write routines using the standard library to compile C# code. So you can easily use C# as a 'scripting' language in your existing application. I've done it before in game development and it proved just as nice a solution as using Python as my scripting language =)
dferraro
C# is *not* an interpreted language. You're right that it's reasonably easy to compile on the fly for scripting purposes, but it's not interpreted. Also, I've found it easier to host dynamic languages like IronPython or IronRuby because (a) the DLR is designed for hosting and (b) you don't have issues about "how does a user script reference another DLL" (in C# it's a compiler switch, but in IR it's just part of the script). Not disrespecting your approach, just an observation!
itowlson
A: 

The main thing I like about Python is its very concise, readable syntax. Though using indentation as a block delimiter can seem strange at first, once you begin to code a lot in the language I find it begins to make sense. Though the core language is quite simple, its more advanced features, e.g. list comprehension, decorators and generators, are rather useful too.

In addition, the Python standard library is just fantastic; its documentation is very well written, and it contains a lot of very useful packages. I also find that there are plenty of good bindings for C libraries, such as PyGTK, Webkit and Qt, to name but a few.

One caveat is that Python, like most dynamic languages, is quite slow in comparison with compiled, statically-typed languages. However, you can easily extend it with C, allowing you to write code requiring better performance in C and the rest in Python.

It's a great language overall, and (for me at least) makes coding more productive and enjoyable.

Donald Harvey
Your first paragraph is good, but I can't bring myself to upvote due to the rest of your answer. You present a false dichotomy between Python and "compiled languages" (whatever that means) -- Python *is* compiled to bytecode, you know. In particular, plenty of other languages have introspection features. The OP mentions C#, which has excellent introspection features due to the .NET platform, and although C# doesn't have a built-in `dir()` function you could write one easily enough.
Daniel Pryden
+2  A: 

I'm an asymmetrical user of both languages, in a sense that I use C# mostly professionally and Python for all my "fun" projects (not that work is never fun, but... you know...)

This difference of context may skew my perspective, including my opinion that they are two distinct types (pun intended) of languages for, generally, distinct purposes.

This said, it may not be a coincidence that Python is, at this point in time, [one of?] the languages of choice for all kinds of cutting edge, somewhat scholarly, technology/science oriented projects. (And BTW, this "scholarly" keyword here doesNOT imply, that Python is a university toy, plenty of "serious" applications in plenty of domains/industry are proof to the contrary). This may be due to several factors:

(I don't develop most points, readily well expressed in other responses)

  • the openness and quasi universal availability of Python (unlike C# !)
  • the lightweight / ease of use / low learning curve
  • the extensive, high quality, "standard" library and the extensiver (and occasionally bum quality, but on the whole available, open-sourced, etc.) additional library.
  • the wide array of open source projects in Python language
  • the relative ease to bind with C/C++ for reusing legacy code, but also for placing performance-critical portions of a project
  • the generally higher level of abstraction of may constructs of the language
  • the multi-paradigms (imperative, object oriented and functional)
  • the availability of practitioners in so many domain of science and technology

and, yes, the

  • "herd mentality effect" mentioned in a remark, possibly in a [self?] deriding way. The fact that a language attracts a broad, "closely knit" community, makes it attractive too, beyond the superficial ("look cool" and such) traits of herd mentality. Put in broader context, sometimes the best technology/language to use is not measured on the its intrinsic merits but on the overall "picture", including the user community.
mjv
+1  A: 

Like any programming language, it is just a tool in the box or a brush by which you may paint your creation. Any creative endeavour requires that the artist loves the tools he uses; otherwise, the outcome suffers. Some people like Python for the same reason others love Perl. Incidentally, I have found that most Python lovers loathe Perl's flexible and expressive syntax. As a Perl lover, I don't hate Python, but consider it to be overly structured and restrictive.

If you ask me, all of these throngs of people who seem to love Python were silently suffering under the tool choices before Python came into being. Some suffered under Perl, others under something else. In other words, I believe that when Python came along, it found a large group of silent sufferers longing for a tool like Python.

I can't program in Python because I can't "think" in Python. I can "think" in Perl, therefore, it is the tool I prefer. The silently suffering mass of, now, Python users seem to have found some long lost salvation. Now if they could only keep their evangelism to themselves :).

I use Python regularly, but I don't loathe Perl. I know lots of Python coders who have nothing against Perl. Don't let a few loud mouths mislead you.
MAK
A: 

Don't get me wrong, I am and will always be a devoted fan of C#.

But sometimes there are things I can't do in C#. lthough C# keeps reducing those gaps, Python is still the language I go to to fill them.

It's dynamic, flexible, powerful, and clean. Lovely language. Whenever I need to script or build dynamic or functional (as in functional programming) software, I go Python.

Randolpho
A: 

For me Python is the most elegant language I've used. The syntax is minimalist (significantly less punctuation than most) and intentionally modeled after the psuedo-code conventions which are ubiquitously used by programmer to outline their intentions.

Python's if __name__ == '__main__': suite encourages re-use and test driven development.

For example, the night before last I hacked together to run thousands of ssh jobs (with about 100 concurrently) and gather up all the results (output, error messages, exit values) ... and record the time take on each. It also handles timeouts (An ssh command can stall indefinitely on connection to a thrashing system --- it's connection timeouts and retry options don't apply after the socket connection is made, not matter if the authentication stalls). This only takes a few dozen lines of Python and it's really is easiest to create it as a class (defined above the __main__ suite) and do my command line parsing in a simple wrapper down inside __main__. That's sufficient to do the job at hand (I ran the script on 25,000 hosts the next day, in about two hours). It I can now use this code in other scripts as easily as:

from sshwrap import SSHJobMan

cmd = '/etc/init.d/foo restart'
targets = queryDB(some_criteria)

job = SSHJobMan(cmd, targets)
job.start()

while not job.done():
    completed = job.poll()
    # ...
    # Deal with incremental disposition of of completed jobs
for each in sorted(job.results):
    # ...
    # Summarize results

... and so on.

So my script can be used for simple jobs ... and it can be imported as a module for more specialized work that couldn't be described on my wrapper's command line. (For example I could start up "consumer" subprocesses for handling other work on each host where the job was successful while spitting out service tickets or automated reboot requests for all hosts reporting timeouts or failures, etc).

For modules which have no standalone usage I can use the __main__ suite to contain unit-tests. Thus every module can contain its own tests ... which, in fact, can be integrated into the "doc strings" using the doctest module from the standard libraries. (Which, incidentally, means that properly formatted examples in the documentary comments can be kept in sync with the implementation ... since they are parts of the unit-test suite).

Jim Dennis
+1  A: 

If you are familiar with the .NET CLR and prefer a statically-typed language, but you like Python's lightweight syntax, then perhaps Boo is the language for you.

Daniel Pryden
+7  A: 

I'm a very heavy user of both C# and Python; I've built very complicated applications in both languages, and I've also embedded Python scripting in my major C# application. I'm not using either to do much in the way of web work right now, but other than that I feel like I'm pretty qualified to answer the question.

The things about Python that excite me, in particular:

  • The deep integration of generators into the language. This was the first thing that made me realize that I needed to take a long, serious look at Python. My appreciation for this has deepened considerably since I've become conversant with the itertools module, which looks like a nifty set of tools but is in fact a new way of life.

  • The coupling of dynamic typing and the fact that everything's an object makes pretty sophisticated techniques extremely simple to implement. It's so easy to replace logic with tables in Python (e.g. o = class_map[k]() instead of if k='foo': o = Foo()) that it becomes a basic technique. It's so normal in Python to write methods that take methods as parameters that you don't raise an eyebrow when you see d = defaultdict(list).

  • zip, and the methods that are designed with it in mind. It takes a while before you can intuitively grasp what dict(zip(k, v)) and d.update(zip(k, v)) are doing, but it's a paradigm-shifting moment when you get there. An entire universe of uninteresting and potentially error-laden code eliminated, just by using one function. Then you start designing functions and classes with the expectation that they'll be used in conjunction with zip, and suddenly your code gets simpler and easier. (Protip: Or itertools.izip. Or itertools.izip_longest.)

  • Speaking of dictionaries, the way that they're deeply integrated into the language. Understanding what a line of code like self.__dict__.update(**kwargs) does is another one of those paradigm-shifting moments.

  • List comprehensions and generator expressions, of course.

  • Inexpensive exceptions.

  • An interactive intepreter.

  • Function decorators.

  • IronPython, which is so much simpler to use than we have any right to expect.

And that's without even getting into the remarkable array of functionality in the standard modules, or the ridiculous bounty of third-party tools like BeautifulSoup or SQL Alchemy or Pylons.

One of the most direct benefits that I've gotten from getting deeply into Python is that it has greatly improved my C# code. I could generally understand code that had a variable of type Dictionary<string, Action<Foo>> in it, but it didn't seem natural to write it. (I use static dictionaries to replace hard-coded logic far more frequently today than I did a year ago.) I have no difficulty understanding what LINQ is doing now, or how IEnumerable<T> and return yield work.

So what don't I like about Python?

  • Dynamic typing really limits what you can do with static code analysis. Not only isn't there a tool like Resharper for Python, in a language where it's possible to write getattr(x, y)() there really can't be.

  • It has a bunch of inelegant conventions. How I would love to be able to go back in time and try to talk GVR out of the idea that lambda expressions should be introduced with the word lambda - it's pretty damning that something as fundamental as lambda expressions should be more concise in C# than they are in Python. The leading and trailing double-underscore convention is horrible, and the fact that people mutely acquiesce to it is testimony to Dostoevsky's observation that man is the animal who can get used to anything. And don't get me started on the fact that a module with the name of StringIO was allowed to get out the door.

  • Some of the features that make Python work on multiple platforms also make it kind of baffling. It's easy to use import, but it's really not easy to understand what the hell it's actually doing. (Where is it looking? What does __init__.py do? Etc.)

  • The amazingly rich library of standard modules is so amazingly rich that it's hard to know what's in it. It's often easier to write a function than it is to find out whether or not there's something in the standard library that does the same thing - I'm looking at you, itertools.chain.

Robert Rossney
This answer sounds like it could have been really useful to a C# developer who might be interested in python. Unfortunately you have to be a Python developer to grasp it. Fail!
AnthonyWJones