getattr(CSharp, "Pythonic") += 1
July 24, 2007 12:47 AM   Subscribe

Hilarious, but earnest: how can I make C# more like Python?

I hate C# very much, but it's a work requirement. I'm used to Python and C (and various intermediate/assembly languages) as a baseline.

Over the years, I've done contracts which required me to use C#, Java and C++, but I despise object orientation as it stands in "Enterprise-class" languages.

Python, on the other hand, provides me with instant reflection, lots of options for dynamic attribute access, freedom from casting (viz. duck typing) and Pyrex, one of the best glue languages outside of C itself (C++/CLI makes me weep with frustration.)

If I'm going to use this language for work — and it's the language that matters — how can I add my own semantic sugar and make it all Pythonic with goodness and happy?

Primarily important to me are: calling SQL stored procedures as if they were methods (think SQLAlchemy,) Python-style reflection, generators (I know I need to implement IEnumerator – but how?) and IL generation (if all else fails.)
posted by electronslave to Technology (18 answers total)
 
Perhaps it would be worth thinking about how if you write C# in such an idiomatic way it might make it more difficult for your successors to maintain your code.
posted by zixyer at 12:58 AM on July 24, 2007 [5 favorites]


Can you use IronPython?
posted by ldenneau at 1:14 AM on July 24, 2007


Don't upset maintenance programmers by making them learn Python in order to manage a C# application.

If you hate C# that much, quit your job and find one that will let you code in Python. I did and I'm 10x happier for it.
posted by cheaily at 1:30 AM on July 24, 2007


Yeah, seriously. IronPython. It's great, and yes, you can compile it to full on assemblies that you can deliver elsewhere.

It is a bit of a pain for *seriously* overtyped stuff, though.
posted by effugas at 2:02 AM on July 24, 2007


"Please help me do my job less well"? Speaking as someone who does a lot of maintenance work: DO NOT WANT.
posted by Leon at 2:17 AM on July 24, 2007


As a fellow Python-lover, I can understand this desire. But I also hang out on comp.lang.python, where occasionally someone asks how they can program Python in a way more like Java. These people are clearly missing the point—by programming in a style developed for another programming language their programs are full of warts and ugliness, and they are missing the features that make Python special.

You want to do the same in C#. Despite being a Pythonvangelist, if I were in your shoes, I would try to learn the better features of C#, or find a new job.

Here's how to do generators in C#, with syntax and keywords "borrowed" from Python.

Your title is a SyntaxError in Python.
posted by grouse at 3:42 AM on July 24, 2007


I can only agree with the above... it sounds like you want to write incredibly bad C# (and will probably end up re-implementing half of the neat stuff built into the language) that someone is going to curse you for a few years down the road when they have to maintain it.

IronPython sounds like a good option. At the very least, it can be decompiled back to C# (and vice-versa) from MSIL so the next guy can convert it to something he's happy with.
posted by jon4009 at 4:58 AM on July 24, 2007


Get used to C#. Its an awkward language, but not all together bad. Don't try to make it into Python.

Alternately, use IronPython.
posted by devilsbrigade at 5:53 AM on July 24, 2007


Or try boo .NET
posted by rottytooth at 5:58 AM on July 24, 2007


Response by poster: First, let me say that I'm absolutely not trying to shoot the guy who maintains my code in the face. Second, C# is required. I tried to hock IronPython from every angle, but it really is the "but we can't go on craigslist and hire a Python programmer if we need one" management factor (yes, I know.)

My Pythonic comment is less "duh, how can I write x language in y" and more sheer terror at the language. For an explanation, type import this into a Python shell.

Ideally, I'd use some obvious features of the language so that I (and my future maintainer) can actually look at my code, know what it's doing and get on with the work. Seems like C#2 has some sugar like the Enumerator thing above.

And now, like Sinistar, I hunger for more. Specifically, has anyone found anything that behaves like SQLAlchemy? Is that even possible with C#?

grouse: How embarrassing! Note: drinking and askme are not a good combination (as with calculus; don't drink and derive.) I really should have made it "import this #please!" or something.
posted by electronslave at 7:56 AM on July 24, 2007


Visual Studio supports that through ADO.NET and strongly typed DataSet objects. Look here and here and here.

It really sounds like your aversion to C# comes from not understanding how to use .NET. I'd suggest getting a good book in .NET programming and architecture, especially ADO.NET. You may find you like it more than you expect.
posted by devilsbrigade at 8:17 AM on July 24, 2007


(Look here for calling stored procs)
posted by devilsbrigade at 8:19 AM on July 24, 2007


I don't want to beat a dead horse, but one of those truisms is "Readability counts".

How can I put this... consider how you would approach the project if you didn't have an ego. Is moving away from standard C# idioms better for the client, the user or the maintenance developer? Or for you?

Everyone who speaks C# speaks ADO.NET. Why add impedance mismatch to the project?
posted by Leon at 8:38 AM on July 24, 2007


You might, conceivably, have better luck pushing to use C# 3.0 at work -- it has a lot of features you'll probably find familiar from more dynamically-typed and functional languages.
posted by inkyz at 9:55 AM on July 24, 2007


See if you can find a python job. Or, see if you can use python in your current job for non critical apps or for things like code generation and the like.

/Python is good but Common Lisp is better.
posted by jockc at 11:56 AM on July 24, 2007


WTF everybody? Answer the question, or don't post.
posted by nicwolff at 12:19 PM on July 24, 2007


@nicwolf:

Sometimes the best answer to "How do I?" is "Don't."
posted by mrbugsentry at 1:02 PM on July 24, 2007


Once you get to know a language well enough, you will come to hate it. This includes Python. Accept that no matter which path you go down, there is misery. It makes it easier to accept the path you are on.
posted by jewzilla at 10:37 PM on July 24, 2007


« Older Traveling in France without a passport   |   Web 2.0 Companies Newer »
This thread is closed to new comments.