Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # class_parent_child.py
- class Parent:
- def __init__(self, x):
- self.x = x
- def method1(self):
- return self.x*2
- def method2(self,*arg):
- if arg:
- return '+++'+''.join(arg)
- return self.x+'!!!'
- class Child(Parent):
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def method1(self):
- return self.x*7
- def method3(self):
- return self.x + self.y
- p = Parent('hey')
- c = Child('hi', 'bye')
- print 'Parent method 1: ', p.method1()
- print 'Parent method 2: ', p.method2()
- print
- print 'Child method 1: ', c.method1()
- print 'Child method 2: ', c.method2()
- print 'Child method 2: ', c.method2('any','thing')
- print 'Child method 3: ', c.method3()
- # what could have been as effective but more simplistic...
- class Parent():
- def __init__(x, y):
- def method1():
- return x*2
- def method2():
- if arg:
- return '+++'+''.join(arg)
- return x+'!!!'
- class Child(Parent):
- def __init__(x, y):
- def method1():
- return x*7
- def method3():
- return x+y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement