Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pyclassheirarchy [2014/10/26 01:52] (current)
Line 1: Line 1:
 +===== Python Derived Class Example =====
 +<​code>​
 +#​!/​usr/​bin/​env python
 +#
 +# example of class heirarchy in Python
 +#
  
 +class Foo:
 +        x = -1
 +        def __init__(self,​ v):
 +                print dir(self)
 +                self.x = v
 +
 +        def show(self):
 +                return self.x
 +
 +class Bar(Foo):
 +        y = -1
 +        def __init__(self,​ v):
 +                Foo.__init__(self,​v)
 +                print dir(self)
 +                self.y = v
 +
 +        def show(self):
 +                return str(Foo.show(self)) + " " + str(self.y)
 +
 +if __name__ == "​__main__":​
 +        f = Foo(3)
 +        print f.show()
 +        b = Bar(20)
 +        print b.show()
 +</​code>​
 +
 +----
 +  * [[pythoninfo|Back to Python]]
pyclassheirarchy.txt ยท Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0