Porting Python to C#

When people start programming in Python, they often mention having to type less: no braces, no semicolons, fewer type declarations etc.

The difference may be more obvious when you go in the other direction, moving from Python to another language. This morning I ported some Python code to C# and was a little surprised how much extra code I had to add. When I’ve ported C# to Python I wasn’t as aware of the language differences. I guess it is easier to go down a notch in ceremony than to go up a notch.

Related post: Plain Python

24 thoughts on “Porting Python to C#

  1. wow you are kidding, aren’t you? If not, then welcome to the real world, with real languages.

  2. @Willen — It’s ridiculous to say that Python isn’t a real language because it’s weakly typed. We done several projects in both C# and Python and (IMHO) strongly-typed languages seem like dinosaurs now.

  3. Willen, what a naive comment. Let’s hope you don’t have to look for work any time soon, because that’s not happening for you with your skillz.

  4. When I’m in the flow of writing either language, I don’t think about their differences. But I find it interesting that the differences are more obvious when porting code in one direction than the other.

    If you prefer Python, could say it’s easier to port C# into Python than vice versa because Python is more expressive. If you prefer C#, could you say that’s because Python is more sloppy.

  5. People who feel threatened by dynamic languages (scared of learning something new? a challenge to what they were taught? weird anyway) often resort to saying they are not “real languages”. Fortunately the commercial successes of languages like Python in large companies and large projects in recent years (although Smalltalk was used *massively* by IBM in the nineties) put paid to that being a valid viewpoint for anyone other than the deliberately ignorant.

    By the way, although these terms are fairly loosely defined (or more to the point *differently* defined) it is more normal to speak of Python as strongly but dynamically typed and C# as strongly but statically typed. Python has a strong typing system that it enforces at runtime. It doesn’t do some of the wonderful things that languages like Perl and Javascript do of just changing your types on the fly, nor what C does by allowing you to treat entities as if they were *anything* through pointers.

    I quite like C#, but the static type system *forces* you into extra layers of architecture that protocols and duck typing (plus inherent dynamism) simply don’t require in systems written in Python.

  6. Michael, your comment about the “wonderful” things Perl can do to your types reminds me of a case study I saw from a company that ported one of the apps from Perl to Python. The new Python code crashed terribly. It turns out some of the Perl functions returning error strings. The calling functions expected numbers, interpreted the error strings as numbers, and went about their merry way. Perl’s type conversion had been hiding a lot of bugs.

    I have mixed feelings about PowerShell’s type conversion. It can be wonderful, hiding the differences between COM and .NET objects etc. But I wonder whether it also hides bugs like the Perl code above did.

  7. I’ve a similar observation moving code from C++ to C#. It seems that for the most part, I write less C# code to accomplish a given task. And yes, for a long time, I considered C# not to be a real language.

  8. So yeah – the point I was trying to make earlier but failed is that there are *two* axes of typing: strong vs weak typing and static vs dynamic typing. From this viewpoint Python is not weakly typed (and *never* untyped as some people say) – it is strongly but dynamically typed. C is statically but weakly typed and C# is strongly and statically typed.

  9. I can not speak for python, I am coding Matlab and C# but I think that new versions of Matlab are very similar to python and these languages are comparable.

    Our project (500k LoC mostly in Matlab) is in-house and as such it grows organically, with new unforeseen features requested on the daily basis. Therefore ~ 30% of the time I spend on refactoring (as a side note there is a known proverb which says that 80% of project cost is maintenance).

    With C# refactoring makes fun, with Matlab being just an ASCII blob, refactoring is a nightmare, the stuff brakes on 100 places and there is no way to know it before runtime.

    So in my opinion the strength of the language lies not on the number of keystrokes on the first typing but on what you can do with the code later.

  10. I agree refactoring can be easier in c#. However, when code is covered by unit tests, many of the errors from refactoring can be caught, plus the entire codebase is protected from more insidious bugs that a compiler can’t catch. I’ve found that python has made me code in a simpler fashion so that I don’t have to depend on static typing (tx for clarification, michael) to refactor. Overall I feel this has been a net plus.

  11. @Mikhail: I’m sure that it’s easier to refactor C# code than Matlab code. And I’m sure part of that is the design of the languages. Being a system language, C# has more structure to help it scale.

    But I wonder how much of the ease of refactoring comes from tool support. I wonder how hard it would be to refactor code in the two languages if MathWorks had Microsoft’s budget and vice verse. That would probably equalize things a little.

    On the other hand, C# is easier to test and refactor than C++ even though Microsoft supports both. Part of that is a matter of priorities; I’m sure MS puts more resources behind C#. But part of it is that C# is easier to write tools for. It’s easier to parse, it has reflection, etc.

  12. @joel: I agree with you that unit tests is the best way to make sure the code is at some extent error-free and conforms the specs. But applying unit tests is the second step after the code has been manually written and you want to write possibly error-free code before beginning to test.

    With Matlab, and I think with all dynamic languages, you have to hold all that things about classes and relations between them in your head. With C# the machine (IDE, compiler) does it for you. It is possible in the first place because C# is statically typed.

  13. @Mikhail
    There are lots of good tools available for working with dynamic languages – and lots that can be discovered about the code through static analysis.

  14. @mikhail – yes, c# code contains much more information than python. Suprisingly, tho, with python, I don’t find myself having to maintain a lot in my memory. Pydev for eclipse gives me code assist and sanely named variables remind of the general type. Python has made me wonder if the benefits of strong typing outweigh the cons… Which is likely a matter a programmer style and project requirements. However, the fact that python and its associated tools are free has tipped the scales on several projects vs .net.

    Btw, isn’t c# adding support for dynamic typing?

  15. Dougal Matthews

    When clicking through I was hoping to find some code or more information. If the project is sizable enough and a direct port, how about some LOC counts? Or perhaps code snippets showing the comparison of some key areas where C# is particularly more verbose.

  16. @Dougal: Sorry, but this post was an off-hand comment based on porting a small amount of code. I was porting the source code that came from my Code Project article that I linked to in the post.

    I’d say one place where C# is particularly verbose relative to Python is defining and passing functions.

    This is a little off-topic, but having access to SciPy makes Python more convenient than C# for me when I’m doing scientific computing. That’s not a reflection of the languages, however. It just happens that Python has a large library that has things I need.

  17. I’m used to python, but recently I am getting into a project in C#. As you seem to have experience with both, I’m wondering if you have some advice/warnings for pythonic people switching into C# and what to expect. I coded a bit in C++ a long time ago but have no experience with java and C#, so it is a whole new game…

  18. Yan: Take a look at anonymous functions. Since they were added to the language later, C# programmers tend to think of them as an advanced feature. But coming from Python, you might expect them. And they can help you write code more like you might be accustomed to writing. Similarly, look at var. C# will let you leave out type declarations if it can infer the types at compile time. Finally, I’d say learn to appreciate C# for what it is and don’t try to make it into Python.

  19. Thanks a lot, that sounds really helpful.

    I like your final advice the best, since I think it defeats the purpose to try to make a language what it is not. That said, from the googling I’ve seen I think I’ll definitely miss the poetic niceness of list comprehensions and all sorts of things python makes so simple…

  20. Hi,
    I feel we have to come out of this Code and architecture and real language, Language porting etc. for years we are talking, developing, Delivering, researching the same.
    We Need something really different. We ae planning for “WIND” AN Interface (NOT AN IDE) That will just record the logic.
    Select your coding templates. your code will be ported to respective language and architecture.
    Let’s talk and innovate something Portable Looking at the histoiry of the software only constant thing is CHANGE!
    Let the application once developed let it be so !
    The applicatioin should be able to port it self (With testing and deployments) to future technology without much efforts!!
    THis is what I am looking for. We have a Blue print any one interested?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>