Python vs C#, Dynamic vs Managed Compiled
Python is a dynamic language, and C# is a managed compiled language. Lets see their common points and differences.
Feature | C# | Python | Notes |
Array boundary check | Yes | Yes | |
Garbage collection | Yes | Yes | C# uses an industrial-standard 3-generation compacting GC; Python uses reference counting and a tree traversing GC. |
Wild pointers | No | No | Since GC is used, no wild pointer is possible. |
Strongly typed | Yes | Yes | C# is statically typed and also has run-time type checking, so type checking is done both at compile time as explicit type declarations and at run-time when doing type-casting. Python is dynamically typed, so no type-checking is done at compile time. All type checking is done at run-time, including whether the object has a function of a specific name that is called at run-time. This brings difficulty to debugging the python program, though. |
Dynamic classes, functions, objects | Partly | Yes | C# can dynamically load classes in a DLL using reflection; Python inherently has dynamic classes, that is, classes as objects [ce1]. |
Interfaces | Yes | Yes | C# requires explicit interface definition and implementation. Since Python is dynamically typed, interfaces are automatically supported. As long as functions of the same name exist in required classes, the calling code works. |
Generics | Yes (in 2.0) | Yes | Generics in C# requires explicit definition. Python is dynamically typed so generics are automatically supported. A class or a function can operate on objects of any types that fit. |
Templates | No | No | Templates are not supported in Python. In C#, the only thing that resembles templates is its generics on fundamental types. The resemblance is in execution efficiency, rather than in syntax and semantics. |
Initial memory consumption | 8MB (1.0) or 16MB(2.0) | 4MB(2.5) | The actual amount of memory in use may be lower. However this affects the system performance if the amount of free memory is tight. |
Built-in fixed-size arrays | Yes | Yes | |
Built-in array lists | No | Yes | This is something that made Python look weird, but is also a thing that made Python very easy-to-use [ce2]. |
Built-in slicing | No | Yes | This is something that made Python interesting and easy-to-use [ce2]. |
Built-in hashtables | No | Yes | This is something that made Python look a little bit weird, but is also a thing that made Python very easy-to-use [ce3]. |
[ce1]
python:
class A(object):
def __init__(self):
print("A.__init__()")
# let B represent class A
B = A
# create a class A object
a = B()
# print the name of class A
print(B.__name__)
def __init__(self):
print("A.__init__()")
# let B represent class A
B = A
# create a class A object
a = B()
# print the name of class A
print(B.__name__)
[ce2]
python:
list = []
list.append("a")
list.append("b")
list += ["c", "d", "e", "f"]
# now `list' contains "a" to "f"
# print ["c", "d"]
print(list[2:4])
# print "f"
print(list[-1])
# print ["f"]
print(list[-1:])
# print ["e", "f"]
print(list[-2:])
list.append("a")
list.append("b")
list += ["c", "d", "e", "f"]
# now `list' contains "a" to "f"
# print ["c", "d"]
print(list[2:4])
# print "f"
print(list[-1])
# print ["f"]
print(list[-1:])
# print ["e", "f"]
print(list[-2:])
[ce3]
dict = {}
dict["Mary"] = "Little Lamb"
dict["Sam"] = "A House In Seattle"
dict["Michael"] = "Cooperated with the rabbit"
dict["Cougar"] = "Was the No. 1"
dict["Maverick"] = "Is the No. 1"
del dict["Mary"]
dict["Mary"] = "Little Lamb"
dict["Sam"] = "A House In Seattle"
dict["Michael"] = "Cooperated with the rabbit"
dict["Cougar"] = "Was the No. 1"
dict["Maverick"] = "Is the No. 1"
del dict["Mary"]