[Django]-What are some of the core conceptual differences between C# and Python?

9đź‘Ť

âś…

” 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

4đź‘Ť

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

3đź‘Ť

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

2đź‘Ť

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

1đź‘Ť

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.

Leave a comment