[Tutor] Simple Question On A Method (in subclass)

Chris Kavanagh ckava1 at msn.com
Sun Oct 23 00:10:19 CEST 2011


Hello, First, thank you for providing this GREAT service, & THANKS to 
everyone who contributes. It's greatly appreciated. . .I'm new to Python 
(2.7, Win XP) & new to programming in general. I have been studying on 
my own for about a month now. I believe I have a good grasp of the basics.

Secondly, I have several questions about this piece of code from 'A Byte 
Of Python' by Swaroop. Hope that's ok to use this code. I guess I could 
try to write a new ex. on my own, but, figured it would be too confusing.

My question is regarding the tell methods in the subclasses,the code 
{SchoolMember.tell(self)}, in the class Teacher & Student. I just don't 
understand what this is doing? Calling the first method {def tell} from 
the parent class, I assume? There is already a print statement in each 
of the subclass {def tell} showing details, why call another print 
statement (from parent class {def tell})?? I know this should be simple, 
but I'm confused. LOL, obviously.

Thank you in advance!






class SchoolMember:
     '''Represents any school member.'''
     def __init__(self,name,age):
         self.name = name
         self.age = age
         print '(Initialized SchoolMember: %s)' %self.name

     def tell(self):
         '''Tell my details.'''
         print 'Name:"%s" Age:"%s"' % (self.name, self.age),

class Teacher(SchoolMember):
     '''Represents a teacher'''
     def __init__(self,name,age,salary):
         SchoolMember.__init__(self,name,age)
         self.salary = salary
         print '(Initialized Teacher: %s)' %self.name

     def tell(self):
         SchoolMember.tell(self)
         print 'Salary: "%d"' % self.salary

class Student(SchoolMember):
     '''Represents a student.'''
     def __init__(self,name,age,marks):
         SchoolMember.__init__(self,name,age)
         self.marks = marks
         print '(Initialized Student: %s)' %self.name

     def tell(self):
         SchoolMember.tell(self)
         print 'Marks: "%d"' % self.marks

t = Teacher('Mrs. Shrividya',40,30000)
s = Student('Swaroop',22,75)

print ##prints a blank line

members = [t,s]
for member in members:



OUTPUT

$ python inherit.py
(Initialized SchoolMember: Mrs. Shrividya)
(Initialized Teacher: Mrs. Shrividya)
(Initialized SchoolMember: Swaroop)
(Initialized Student: Swaroop)

Name:"Mrs. Shrividya" Age:"40" Salary: "30000"
Name:"Swaroop" Age:"22" Marks: "75"


More information about the Tutor mailing list