Sunday, September 5, 2010

C and Python: A Language Faceoff

1. Data Types
    Python is a good example for a dynamically typed programming
    language. It means that, there are no need for type declarations. Any data
    can be initialized directly and the corresponding type will be automatically
    assigned and understood by the Python interpreter.
    >>>a = 12
    >>>c = 'hello'
    
    C is a statically typed language. We need to first declare the variable.
    Only then are we allowed to define it. The C compiler will understand
    the type of the data and assign suitable storage space only when we
    declare it.
    $ int a, b=2;
    $ char c = 'h';
    $ a=10;

2. Array Indexing
    The mechanism of array indexing in C is as follows:
    array index is given by 'base address + offset'
    Therefore, how large be the array length, C compiler only needs to add
    the offset to the base address to access the required array location.

    In Python, the situation is different. It is compulsory for its interpreter to
    follow 2 rules:
    i. check whether current array index is out of bound
    ii. access that index location

    In short, its even possible to go out of bound in C. Garbage values in
    that locations will be returned. Thats all. But Python is very
    conservative. You specify an array length, you better stay inside it. No
    bending rules.

    Finally, the outcome. As you can guess, array indexing in C is blazing
    fast. If we work on the same array in Python, we may have to wait.
    Maybe really long.

3. Dynamic Allocations
    On the outset, i take the privilege to assume you know what dynamic
    allocations are.

    They are very troublesome in C. Simply speaking, if you do an  
    malloc(), then use the same pointer to do another malloc(), you will
    lose control of the firstly allocated memory block forever. You can't
    use it anymore. So be wary. Thats all you can do.

    For this matter, Python is awesome. It will take care of such
    unhandled allocations of memory automatically. Its called garbage 
    collection or automatic memory management. We can say that, each
    allocated block has a reference count, which has the value of the
    number of pointers currently pointing to that particular block. This
    count, is incremented or decremented automatically and accurately.
    There is one consequence, though. The compilation time will be 
    non-deterministic because we cant predict when and how Python
    attempts this.

4. Indentation
    In short, indentation is compulsory in Python. The intermezzo coding
    style proposes 4 spaces. While the Google coders are seen to be using
    only 2.
  
    In C, it is just the other way. Here, it is only a matter of user 
    readability. The C compiler doesn't care.

5. Functions
    In Python, all the functions, both built-in and user-defined, are first 
    class. A function is said to be first class, if it can be treated just as a
    variable. It means that, in Python, functions can be passed as
    parameters, and returned too. Coool, eh? If so, how about knowing
    that this concept can be extended to creating higher order functions?
    They are functions which operate on other functions, e.g. integration,
    differentiation. Three built-in higher order functions in Python are
    map, filter and reduce.

    In C, its just the contradictory. Functions are functions, variables are
    variables. No mixing up.

6. Pointers
    They are the most awesome weapon in C. You can do powerful things
    with them, that can be either only dreamed of or very hard to
    accomplish in other languages. But as powerful they are, be very
    careful with the memory manipulations you can do with them, or else
    you are a goner.

    They are not as such implemented in Python. But some similarity can be
    felt in some operations that can be performed. For a brief idea, refer to
    "Pointers" in Python.

7. Assignments in Expressions
    In C its possible to write,
    $ int a, b=1;
    $ if ((a=12)>b)
    $         print (" %d \n", a+b);

    Well, its not possible in Python. Only logical equality (==) can occur
    inside an expression.

No comments:

Post a Comment