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

Dave Angel d at davea.name
Sun Oct 23 00:59:45 CEST 2011

On 10/22/2011 06:10 PM, Chris Kavanagh wrote:
> 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:
> $ 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"
Welcome to the python-tutor list.  We are all volunteers here, and most, 
like me, ask questions as well as answering them.  it's a two-way street.

The whole point of subclassing is to share either code, data, or both.  
In this example, the amount of shared data is just the name and age, and 
the shared method is tell().  Because the parent knows how to 'tell' its 
information, the child doesn't need to.  So instead of altering the 
prints in both the child classes to print name & age, you let the common 
code in the parent handle it.

It becomes more clearly a win when you have much more data, or much more 
complex methods involved.  But you have to start simple.

BTW, there were some transcription errors in the email.  For example, 
the code as written would be using a separate line for Salary on Mrs. 
Shrividya's record.   And you're missing the content of the for member 
in members: loop.   No problem, but it might have affected our 
discussion.  Did you retype it all, or was it just a glitch?  Presumably 
you know how to copy/paste from and to a console prompt?


More information about the Tutor mailing list