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

Dave Angel d at davea.name
Mon Oct 24 00:22:18 CEST 2011


(please do REPLY-ALL, or at least add the mailing list to the recipient 
list.  Otherwise, only one person will see the message.  I'm forwarding 
it to the group with my comments added.)

-------- Original Message --------
Subject: Re: [Tutor] Simple Question On A Method (in subclass)
Date: Sun, 23 Oct 2011 16:53:40 -0400
From: Chris Kavanagh <ckava1 at msn.com>
Organization: Home Office
To: d at davea.name



On 10/22/2011 6:59 PM, Dave Angel wrote:
>
>> 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!
>>
>>
>>    <SNIP>

> 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?

Chris Kavanagh said:

Thanks to both Alan Gauld & Dave Angel for the help!!!

I'm not sure how the transcrption errors happened, I copied & pasted.
Must have not copied the entire thing. I'll correct it below.

Speaking of the last line of code, I have a question about that also.
The last line should have been (without my error) {member.tell()}.
My question is, why couldn't this last line have been {print member}??
Every example of an iterator I've seen until this point of my learning,
has had a print statement then the item variable. Instead, Swaroop uses
the item variable to call the function {tell()}. This is sorta confusing
to me. What if there was no function to print out the Student and
Teacher variable?? How would the iterator been printed out in that
case?? Thanks again, in advance. Here's the correct code.


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:
	member.tell() # works for both Teachers and Students
				
				

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"
				
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
You always have a choice of where to put useful code.  if you put it in 
member functions, it can be shared by all the callers.  There's no right 
or wrong way to do it.  The only "rule" I can think of is not to mix 
significant calculations with prints.  Make them two different methods, 
so the user of the class can use them independently.

If you had no class member like tell(), one choice would be to enumerate 
the members yourself.  So you could do something like:

      print member.name, member.age, member.marks, member.salary

With some formatting.  But not all members have all these fields, so 
you'd have to do some conditional testing, and it could get quite complex.

--

DaveA





				


More information about the Tutor mailing list