Python Tutorial

Friday, August 20, 2010

python import class

'''
   saved this class named "A.py"
'''
def printStringNTimes(s,n):
    for i in range(0,n):
        print s

def stringReverse(s):
    return s[::-1]

def stringLength(s):
    return len(s)




'''
   This code use class import syntax
    - must import class "A"
    - Not need to create any object, just use all functionality of class
    - call you desired function by "classname.finction name" 
         (example : A.printStringNTimes("Python",5))
    - put class A.py and current class on same folder
'''
import A
'''
   print a string n times
'''
A.printStringNTimes("Life is very easy with Python",5)
'''
   get length os a string
'''
print "Length :",A.stringLength("Life is very easy with Python")
'''
   reverse a string
'''
print "string reverse :",A.stringReverse("nohtyP htiw ysae yrev si efiL")





Output:
Life is very easy with Python
Life is very easy with Python
Life is very easy with Python
Life is very easy with Python
Life is very easy with Python
Length : 29
string reverse : Life is very easy with Python

0 comments:

Post a Comment