views:

459

answers:

5

Hi everyone,

I'm new to Python, coming from a C# background and I'm trying to get up to speed. I understand that Python is dynamically typed, whereas C# is strongly-typed. -> see comments. What conceptual obstacles should I watch out for when attempting to learn Python? Are there concepts for which no analog exists in Python? How important is object-oriented analysis?

I believe answers to these and any other questions you might be able to think of would speed up my understanding Python besides the Nike mentality ("just do it")?

A little more context: My company is moving from ASP.NET C# Web Forms to Django. I've gone through the Django tutorial and it was truly great. I need to get up to speed in about 2 weeks time (ridiculous maybe? LOL)

Thank you all for your time and efforts to respond to a realllly broad question(s).

+3  A: 

There are a lot of differences between C# and Python; rather than dwell on the individual differences, it's probably better just to look at how Python works using a guide such as Dive Into Python. And remember, while Python allows you to do OOP very well, it doesn't constrain you to OOP. There are times when just plain functions are good enough (Django views being a good example).

There are also numerous conceptual differences between WebForms and Django. Django is more in tune with HTTP - there's no muddling of what happens client-side and what happens server-side. With a typical WebForms application, client side events often trigger server-side code using postbacks. Even with the ASP.NET Ajax framework, it's an environment which offers less control than you sometimes need. In Django, you achieve the same effect using client-side libraries using e.g. YUI or jQuery and make Ajax calls yourself. Even though that kind of approach doesn't hold your hand as much as say the ASP.NET approach, you should be more productive with Django and Python to make the latter an overall net positive. ASP.NET aims to make things more familiar for developers accustomed to WinForms and other desktop development environments; while this is a perfectly reasonable approach for Microsoft to have taken (and they're not the only ones - for example, Java has JSF), it's not really in tune with HTTP and REST to the same extent. For an example of this, just take a look at how constraining ASP.NET URLs are (pre ASP.NET MVC) as compared to Django URLs.

Just my 2 cents' worth :-)

Vinay Sajip
Thank you very much! I enjoyed your response, and I agree that dwelling on the individual differences won't give me a complete picture; however, I would like to know what to watch out for, which I believe might be best framed in regards to the individual differences. Also, I just think it would fun to understand different approaches between C# and Python.
mkelley33
In your first paragraph, do you mean C# is bound to OOP? If so, I don't agree.
Dykam
+1  A: 

The conceptual differences are important, but mostly in how they result in different attitudes.

Most important of those are "duck typing". Ie, forget what type things are, you don't need to care. You only need to care about what attributes and methods objects have. "If it looks like a duck and walks like a duck, it's a duck". Usually, these attitude changes come naturally after a while.

The biggest conceptual hurdles seems to be

  1. The significant indenting. But the only ones who hate it are people who have, or are forced to work with, people who change their editors tab expansion from something other than the default 8.

  2. No compiler, and hence no type testing at the compile stage. Many people coming from statically typed languages believe that the type checking during compilation finds many bugs. It doesn't, in my experience.

Lennart Regebro
Actually the python indent standard is 4 spaces. http://www.python.org/dev/peps/pep-0008/Just use an editor that allows you to have "fake" tabs.
DoR
Tab expansion != default indent.
Lennart Regebro
+3  A: 

duck typing

I think the main thing that sets c#/java from python is that there is often no need for interfaces. This is because python has ducktyping.

class Duck(object):
    def quack(self):
        print "quack"

class Cat(object):
    """Cat that specializes in hunting ducks"""
    def quack(self):
        print "quack"

duck = Duck()
cat = Cat()

def quacker(something_that_quacks):
    something_that_quacks.quack()

quacker(cat) #quack
quacker(duck) #quack

As long as an object has the method quack its OK to use it to call quacker. Duck typing also makes design patterns more easy to implement. Because you don't need to write interfaces and make sure objects are of the same type.

MrHus
The indentation needs to be fixed otherwise your example won't run.
jpartogi
+2  A: 

You said that Python is dynamically typed and C# is strongly typed but this isn't true. Strong vs. weak typing and static vs. dynamic typing are orthagonal. Strong typing means str + int doesn't coerce one of the opperands, so in this regard both Python and C# are strongly typed (whereas PHP or C is weakly typed). Python is dynamically typed which means names don't have a defined type at compile time, whereas in C# they do.

Alex Gaynor
Thanks for the clarification.
mkelley33
+6  A: 

" I understand that Python is dynamically typed, whereas C# is strongly-typed. "

This is weirdly wrong.

  1. Python is strongly typed. A list or integer or dictionary is always of the given type. The object's type cannot be changed.

  2. Python variables are not strongly typed. Indeed, Python variables are just labels on objects. Variables are not declared; hence the description of Python as "dynamic".

  3. C# is statically typed. The variables are declared to the compiler to be of a specific type. The code is generated based on certain knowledge about the variables use at run-time.

Python is "interpreted" -- things are done at run-time -- little is assumed. [Technically, the Python source is compiled into byte code and the byte code is interpreted. Some folks think this is an important distinction.]

C# is compiled -- the compiler generates code based on the declared assumptions.


What conceptual obstacles should I watch out for when attempting to learn Python?

None. If you insist that Python should be like something else; or you insist that something else is more intuitive then you've polluted your own thinking with inappropriate concepts.

No programming language has obstacles. We bring our own obstacles when we impose things on the language.

Are there concepts for which no analog exists in Python?

Since Python has object-oriented, procedural and functional elements, you'd be hard-pressed to find something missing from Python.

How important is object-oriented analysis?

OO analysis helps all phases of software development -- even if you aren't doing an OO implementation. This is unrelated to Python and should be a separate question.

I need to get up to speed in about 2 weeks time (ridiculous maybe?)

Perhaps not. If you start with a fresh, open mind, then Python can be learned in a week or so of diligent work.

If, on the other hand, you compare and contrast Python with C#, it can take you years to get past your C# bias and learn Python. Don't translate C# to Python. Don't translate Python to C#.

Don't go to the well with a full bucket.

S.Lott
Thank you for the correction.
mkelley33
I'm marking this as the answer, because the Zen is strong with this one, and will probably lead me to enjoy Python in an open-minded way. That is, I won't be encumbered by unnecessary comparisons. I would say I shouldn't have asked such a broad question in the first place, but my folly in assuming that comparison was necessary between two languages has convinced me (and hopefully others) to learn Python, applying concepts instead of language experience and/or comparisons. Thank you very much for the insight and help.
mkelley33