''' This code use python class syntax - save this class named "MyClass.py" ''' class MyClass(): # Class constructor def __init__(self,data): self.data=data def showSring(self): print self.data def reverseString(self): return self.data[::-1] if __name__=='__main__': myClass=MyClass("Life is very easy Python") myClass.showSring() print myClass.reverseString()
''' This code use class object -save this code named "AnyName.py" ''' import MyClass class MyClassUser(): def __init__(self,data): self.data=data self.myClass=MyClass.MyClass(self.data) def callMyClass(self): self.myClass.showSring() print self.myClass.reverseString() if __name__=="__main__": myClassUser=MyClassUser("Life is very easy with Python") myClassUser.callMyClass()
Must save class "Myclass.py" and "AnyName.py" in same folder then run "AnyName.py"
Output:
Life is very easy with Python nohtyP htiw ysae yrev si efiL
0 comments:
Post a Comment