[Tutor] Class Inheritance

Lamonte Harris pyth0nc0d3r at gmail.com
Thu Sep 13 22:03:57 CEST 2007


class A:
   def __init__(self,x,y):
     self.x = x
     self.y = y

   def save(self,fn):
     f = open(fn,"w")
     f.write(str(self.x)+ '\n') # convert to a string and add newline
     return f             # for child objects to use

   def restore(self, fn):
     f = open(fn)
     self.x = int(f.readline()) # convert back to original type
     self.y = int(f.readline())
     return f

class B(A):
   def __init__(self,x,y,z):
     self.z = z

   def save(self,fn):
     f = A.save(self,fn)  # call parent save
     return f         # in case further children exist

   def restore(self, fn):
     f = A.restore(self,fn)
     self.z = int(f.readline())
     return f

In the class B,  I'm not understanding the A.__init(self,x,y) part.  So its
initializing the class A, and basically you can use the A class like normal?
Part im confused about is the self.z, does that belong to the class A or
class B?  Else I think I'm understanding it correctly.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070913/2102617a/attachment.htm 

More information about the Tutor mailing list