Wednesday, January 18, 2012

Python Note 3 (Functions)

We can define number of functions in same python script.
def cube(x) :
 return x**3

def maxVal(x,y) :
 if x > y :
  return x
 else :
  return y

print (cube(3))
print (maxVal(12,22))

Lets see how to import functions in another python script.
There are 2 importing methods
  1. import a single function
  2. import all functions under a "namespace"(python script)
  • import a single function
assume we have implemented a function called factorial() in a python script named as .
from mylib import factorial
print (factorial(4))

So we can call function directly.

Other way
import mylib
print (mylib.factorial(4))

In this method there may be some anomalies if we are going to import 2 functions with same name and signature in different scripts.


Post a Comment

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis