Tuesday, August 16, 2005

Reading a file and then interactive mode

This was is quite simple.

$python -i scriptname

Importing modules in a loop

I first wrote up the unsafe way of doing it using exec

for script in ['power','cube']:
exec "import " + script

But as using execs could be a bad habit ,the better way would be to use __import__


for script in ['power','cube']:
__import__(script)

Saturday, April 30, 2005

Python Functions

Here is how you define it...
Well i had a hard time putting a "," after the format strings..
i am a C guy and i am proud of it :)


#!/usr/bin/env python

def countChicks(chick,count):
for i in range(count):
print "Here is %s number %d" % (chick,i)


countChicks("My chick",5)



Now a cool thing in python is that if you have a list or other structures(well i dont know any other so we will stick to a list),you can make your function as one of the objects.
After the code chunk for the function def is eval'ed ,we have an function object called countChicks .So the same thing can be rewritten as.

#!/usr/bin/env python

def countChicks(chick,count):
for i in range(count):
print "Here is %s number %d" % (chick,i)


mylist = (countChicks,"My chick",5)
mylist[0](mylist[1],mylist[2])


Yo