Python Design

I will add a series of these posts which are just a bunch of notes that I am going to create for learning Python in depth from one of the Google talks on Python.

  • Everything is run time* (compile time* too is runtime)
    • somethings are cached (.pyc files)
  • Execution happens in namespace
    • modules, functions and classes all have their own namespace.
  • Modules are executed top to bottom
    • just like a script
  • def and class statements are runtime

Function creation

  • def statement creates a function
  • Code is compiled at compile time
    • into a code object
    • The code for the function will be compiled into a code object, which is a separate object from whatever code the rest of the program is compiled into
  • Wrapped in a function object at def time
    • When the def statement is executed the code object is wrapped into a function object along with whatever arguments it takes and any defaults to those arguments
  • Arguments defaults evaluated then too
  • def is primarily an assignment statement
    • essentially if you look at the bytecode then it is the same bytecode as the assignment operation

def func(arg1, arg2=func2()):
    print "entering func1"
    def inner_func(arg3=arg2):
        print "entering inner_func"
        return arg1, arg3
    arg1 = arg2 = None
    return inner_func

When the def statement is reached that whole line gets evaluated including the default arguments and the rest of the program which is

    print "entering func1"
    def inner_func(arg3=arg2):
        print "entering inner_func"
        return arg1, arg3
    arg1 = arg2 = None
    return inner_func

has been compiled because the compile time is in the past but is not being executed until you call the function. When func() is executed the inner func is evaluated till the def statement along with the defaults but the rest is compiled and also the defaults are evaluated at this time so whatever arg2 was is assigned to arg3 and not arg3 will not get that value of arg2 when you call inner_func so if the value of arg2 changes later on the value of arg3 will still be that value of arg2 when the def statement was reached.


Class Creation

Pretty similar to functions but a little different as well

  • Class statements executes suite
    • in a separate namespace
    • Class statement is followed by a block of code that is executed when the class statement is reached.
  • Class object is created out of the dict
    • name space becomes attribute space
  • Methods are just functions inside a class
    • Special wrappings happens automatically at attribute level
    • self is passed implicitly but received explicitly
  • Class body is normal code block.

* There is a pretty clear answer on stackoverflow regarding this concept which I would like to add here.

Run time and compile time are different stages in the running of a computer program. It is quite a difficult concept to learn and it is best approachable if you ask it in terms of these questions.

  1. What invariants the program satisfy.
  2. What can go wrong in this phase.
  3. If the phase succeeds what are the post conditions.
  4. What are inputs and outputs if any.

Compile time

  1. The program need not satisfy any invariant, in fact it need not be well formed at all.
  2. Things that can go wrong here are
    1. Syntax errors
    2. Type checking errors
    3. Compiler crashes
  3. If the compiler succeeds we know that the program is well formed and it is possible to run the program.
  4. Inputs and Outputs?
    1. Inputs are the program itself along with the headers or any other libraries to be imported
    2. Output is the assembly code or relocatable object code or executable. If the program has errors then outputs are the bunch of those error messages.

Run time

  1. We know nothing of program invariants, they can be anything that the programmer specifies
  2. Things that can go wrong here are
    1. Division by zero
    2. Dereferencing a null pointer
    3. opening a file which is not already there
  3. If run time succeeds the program finished without crashing
  4. Inputs and outputs are entirely on the programmer.

One thought on “Python Design

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s