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