Thursday, April 22, 2010

Class Inheritance

Use __class__, __bases__ and __dict__ for sub and super class

inheritance learn python programming language, Python is very simple. Now using __class__, __bases__ and __dict__ for sub and super class. Try making a super class and child class. Examples like this:



class super:
         def hello(self):
               self.data1 = 'spam'

class sub(super):
         def hola(self):
                self.data2 = 'eggs'

X = sub()
X.__dict__
{}

print X.__class__

print sub.__bases__

print super.__bases__


Y = sub()

X.hello()
print X.__dict__

X.hola()
print X.__dict__

print sub.__dict__

print super.__dict__

print sub.__dict__.keys(), super.__dict__.keys()

print Y.__dict__

print X.data1, X.__dict__['data1']

X.data3 = 'toast'
print X.__dict__

X.__dict__['data3'] = 'ham'
print X.data3

print X.__dict__
print X.__dict__.keys()

print dir(X)
print dir(sub)
print dir(super)

Result

No comments:

Post a Comment