Advertisement
here2share

# class_parent_child.py

Mar 18th, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. # class_parent_child.py
  2.  
  3. class Parent:
  4.     def __init__(self, x):
  5.         self.x = x
  6.     def method1(self):
  7.         return self.x*2
  8.     def method2(self,*arg):
  9.         if arg:
  10.             return '+++'+''.join(arg)
  11.         return self.x+'!!!'
  12.  
  13. class Child(Parent):
  14.     def __init__(self, x, y):
  15.         self.x = x
  16.         self.y = y
  17.     def method1(self):
  18.         return self.x*7
  19.     def method3(self):
  20.         return self.x + self.y
  21.  
  22. p = Parent('hey')
  23. c = Child('hi', 'bye')
  24.  
  25. print 'Parent method 1: ', p.method1()
  26. print 'Parent method 2: ', p.method2()
  27. print
  28. print 'Child method 1: ', c.method1()
  29. print 'Child method 2: ', c.method2()
  30. print 'Child method 2: ', c.method2('any','thing')
  31. print 'Child method 3: ', c.method3()
  32.  
  33. # what could have been as effective but more simplistic...
  34. class Parent():
  35.     def __init__(x, y):
  36.         def method1():
  37.             return x*2
  38.         def method2():
  39.             if arg:
  40.                 return '+++'+''.join(arg)
  41.             return x+'!!!'
  42.  
  43. class Child(Parent):
  44.     def __init__(x, y):
  45.         def method1():
  46.             return x*7
  47.         def method3():
  48.             return x+y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement