Python vs C#, Dynamic vs Managed Compiled

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
NoNo
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__)


[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:])


[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"]


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值