Custom Search

Thursday, June 9, 2011

python howto call parent class method

python howto call parent class method


class A:
def __init__(self):
self.x = 100

def test(self, a):
print "--self----a--1-", self, a
print "--self.x----a--1-", self.x

class B(A):
def __init__(self):
#A.__init__(self)
self.x = 200
#pass

def test(self, a):
A.test(self, 10) #<----------
print "--self----a---2-", self, a
print "--self.x----a--1-", self.x

b = B()
b.test(5)

OUTPUT
========
$ python p.py
--self----a--1- <__main__.B instance at 0xb7f39acc> 10
--self.x----a--1- 200
--self----a---2- <__main__.B instance at 0xb7f39acc> 5
--self.x----a--1- 200

No comments:

Post a Comment