Use __class__, __bases__ and __dict__ for sub and super class : Class Inheritance « Class « Python






Use __class__, __bases__ and __dict__ for sub and super class

Use __class__, __bases__ and __dict__ for sub and super class
 
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)


           
         
  








Related examples in the same category

1.Print out class treePrint out class tree
2.Class inheritedClass inherited
3.Inherited methodInherited method
4.Polymorphism: override the function from base class
5.Definition and test function for class Circle which is based on Point class
6.Derived class inheriting from a base class.
7.Multiple Inheritance
8.inheriting from multiple superclasses
9.Use Inheritance to add more features to a class
10.class extending dict type
11.Class inheritance: inherit member variable override functionClass inheritance: inherit member variable override function