0

the question is within this example: (using *'s)

#!/usr/bin/python
# Filename: inherit.py

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

Why are the subclasses re-doing the same arguments in the __init__ and using the superclass __init__ as well? Shouldn't the subclass only need to istantiate the salary along with using the SchoolMember's name and age?

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by thatoneguyx
0

These are the name, age arguments of that particular instance linking back to the superclass. In other words, each inherited superclass is unique to that instance.

0

The reason SchoolMember's __init__ is called to define self.name=name and self.age=age. You could also do this in the Teacher and Student classes. It doesn't matter where it happens as SchoolMember is inherited. I have changed the Student class to illustrate.

#!/usr/bin/python
# Filename: inherit.py

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.name = name
                  self.age = 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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.