Python Variables

Following up on my previous post, in this one I will writing mostly on variables, scopes, modules


  • Variables are name, not containers
    • Everything is an object
    • Everything is a reference
    • Variables are neither
    • They are not allocated memory that holds data, they are neither objects or references they are only labels
  • Everything concrete is an object
    • Whenever you hold an object, whenever you store it anywhere you are storing a reference, you dont own an object, you just own their reference.
  • Everything that holds anything, holds references
  • Variables refers to objects
    • namespaces maps names to objects


  • 3 Scope rule
    • names are either local or global or built ins
  • Local names defined by assignment unless ‘global’ definition says otherwise
    • local means existing in current class, function, global means existing in current module and not existing everywhere
    • builtins is just a special namespace used by builtin functions
    • if you have a global of the same name as a builtin the compiler will find the global instead of the builtin


  • They have their own namespace, keeps the rest of the modules tidy
  • Executed on first import, namespace becomes attributes
  • Then cached in sys.modules
  • import is syntactic sugar, it calls in the following function
    • mod = __import__(“mod”)
  • sys.modules doesn’t have to contain only modules
    • it is just a mapping of names to whatever object you want import to return
  • If you map None to a name then it will raise import error

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.

Book Review “Building Impressive Presentations with impress.js”

“Building Impressive Presentations with impress.js” is an insightful book for anyone who wants to understand the concepts of impress.js (a Javascript library for creating beautiful 3D presentations). What I really like about this book is the conversational style of writing by the author and that the author starts off with the very basic concepts and each chapter later on builds upon those concepts, thus helping the reader grasping the basics of topics like axis, rotations, translations in the initial chapters and then helping them plan and create their own websites and portfolios. There are code samples provided in the book which is really helpful especially for the beginners. I would highly recommend this book to anyone who wants to get over the dreary presentations and create something truly magical using this library and their creativeness.
So do get your copies of this ebook from here

Re-registration of vehicles in a different state in India

These days a lot of people have to relocate to a different state in India because of work and they tend to take their vehicles with them. Traffic sleuths are always looking for a out of state vehicles to slap a huge fine and most people tend to bribe their way out because seriously who wants to get involved in matters concerning RTO and pay the whole road tax when they are actually for a short term period in another city, but all these vows can be avoided, a lesson I learnt the very hard way. It so happened that I moved to Pune and I didnt know about the correct procedure, I got caught a couple of times and then I couldnt take it anymore so I decided to get my vehicle registered in Maharashtra(as suggested by a dalal whom I met at Pune RTO). Re-registering a vehicle is not actually required but those dalal(read bastards) would say its better to get it registered to churn out more money from your pockets. The right thing to do here is just pay the road tax for another state, this sounds crazy as you are not going to stay permanently in another state but what most people don’t know is that there is a provision to get a refund if the stay is for a short time and the refund is calculated for the number of months stayed. So all you have to do is to get a NOC(no objection certificate) which is required so that the RTO knows that their are no pending cases on that vehicle or if the vehicle actually belongs to you. Getting a NOC is fairly easy, all you have to do is to go to the RTO where you vehicle is registered and they will tender the NOC in a day or two without any charges. Next when you arrive in another city then within a month pay the road tax. The papers that you need for paying the road tax are as follows

  • NOC
  • PUC
  • Insurance
  • License 
  • RC book

Well and thats all their is to it. You can get everything done in a days time just keep in mind not to contact any dalals in between or you will have to pay extra and it might take more time and a lot of headaches. Also if you dont pay the road tax within a month when the NOC  was tendered then the RTO will charge you extra for the months you have stayed in another city. So no re-registration is required all you have to do is to pay the road tax.

Scoping in Python – The LEGB Rule

Actually, a concise rule for Python Scope resolution, from Learning Python, 3rd. Ed.. (These rules are specific to variable names, not attributes. If you reference it without a period, these rules apply)

LEGB Rule.

L. Local. (Names assigned in any way within a function (def or lambda), and not declared global in that function.

E. Enclosing function locals. (Name in the local scope of any and all enclosing functions (def orlambda), form inner to outer.

G. Global (module). Names assigned at the top-level of a module file, or declared global in a def within the file.

B. Built-in (Python). Names preassigned in the built-in names module : open,range,SyntaxError,…

So, in the case of

class Foo:
   def spam.....
      for code4..:

The for loop does not have it’s own namespace. It would look in the LEGB order,

L : local, in the current def.

E : Enclosed function, any enclosing functions(if def spam was in another def)

G : Global. Were there any declared globally in the module?

B : Any builtin x() in Python.



source- stackoverflow

TTY – TeleType

Read, heard and ever wondered what a tty is. Well it stands for teletype and it is a command used in the shell. Basically it gives information about the terminal that a user is currently at. TTY has historic significance as they were used in the first computers which were TeleType terminals and these were essentially remote controlled typewriters. When you enter the command ‘tty’ in the shell without the quotes you will receive a response ‘dev/pts/1’ or some other number which essentially tells you that you are using device /pts/1.

Installing Guest Additions and DKMS on Centos.

I was trying to install guest additions on my Centos operating system but faced a lot of problems. Though the idea was very simple as all you have to do was to install DKMS package  on your centos operating system and run the install virtual box guest additions setup, but the main problem is that dkms package is not available on your centos,  it is a third party repository. So I believe there are a lot of new users who face this issue (I being one of them).

Following steps will help in installing guest additions on your centos.

Step1: update everything( though not really required but still I took this step 1st)

Step2: make a directory rpm using the following commands and go in that directory and download the rpm package from this link

$ mkdir rpm

$ cd rpm

$ wget

$ rpm -i rpmforge-release-0.5.2-2.el5.rf.*.rpm

$ yum install htop

now if you get an error something like this

error: Failed dependencies:
rpmlib(FileDigests) <= 4.6.0-1 is needed by rpmforge-release-0.5.2-2.el6.rf.i686
rpmlib(PayloadIsXz) <= 5.2-1 is needed by rpmforge-release-0.5.2-2.el6.rf.i686

That means you have installed your centos virtual machine from cloudera which is centos5 and you have downloaded rpm package for centos6 so all you have to do is to change that package and download package for centos5. You can also check if you are running a 32 bit machine or a 64 bit machine as there are two packages one is for 32 bit machine and the other for 64 bit. To check which machine you are running just type the following command

$uname -i

if you get i386 or i686 that means you are running 32 bit machine and if you get x86_64 that means you are running a 64 bit machine.

Step3. Install kernel-devel

$ sudo yum install kernel-devel

Step4. So almost everything is done and you are ready to install  dkms package

sudo yum install dkms

if everything goes fine dkms package will install successfully, without any issues.

Step5. This will be the final step

Insert VboxGuestAdditions.iso and go to the folder which will be probably in


and run the following command

$ sh ./


This will successfully install Guest Additions on Centos.