Re: [Tutor] Creating subclasses (newbie)

Magnus Lycka magnus at
Thu Jun 24 10:07:05 EDT 2004

Adam wrote:
> I have a subclass I want to create - my intuition told me
> that it would be done like this:
> class MainClass:
> 	class SubClass:
> 		code...
> 	subclassinstance = SubClass()
> mainclassinstance = MainClass()

I'm afraid your intuition misled you completely. ;)

To be completely honest, it doesn't seem that you quite 
understand what subclasses are for and how they are used if 
you think they would be made inside the base class.

I guess the word subclass is really misleading. Maybe it's
not really what you actually need. Feel free do discuss your
design with us.

A subclass is a specialization of a base class. For instance
an employee is a kind of person, and a manager is a kind of
employee. The more abstract and generic code, i.e. the base 
class code, should only deal with the abstract and generic
issues, and should know nothing about the more concrete and
specialized uses of itself. In other words, the base class
shouldn't know which subclasses it has.

It's common that base classes are part of standard libraries
and third party libraries, and that applications programmers
subclass these classes in their own modules.

This is an example of subclassing, where Employee is a sbclass
of Person, and Manager is a subclass of Employee. Note that
Person inherits from the standard base class 'object'.

class Person(object):

    def __init__(self, name): = name

    def __str__(self):
        return "A Person called %s" %

class Employee(Person):
    dept = 'Unknown'

    def set_dept(self, dept):
        self.dept = dept

    def __str__(self):
        return "An Employee called %s working at %s department" % (
                       , self.dept)

class Manager(Employee):

    def __init__(self, *args):
        self.subjects = []
        super(Employee, self).__init__(*args)

    def add_emp(self, emp):

    def remove_emp(self, emp):

    def __str__(self):
        emps = [str(emp) for emp in self.subjects]
        return "%s at %s department is manager for %s" % (
       , self.dept, ",".join(emps))        

>>> p = Person('Peter')
>>> print p
A Person called Peter
>>> e1 = Employee('Eric')
>>> e1.set_dept('Sales')
>>> e2 = Employee('Ed')
>>> print e1
An Employee called Eric working at Sales department
>>> print e2
An Employee called Ed working at Unknown department
>>> e2.set_dept('Construction')
>>> print e2
An Employee called Ed working at Construction department
>>> m = Manager('Mike')
>>> m.set_dept('Sales')
>>> e3 = Employee('Emma')
>>> e3.set_dept('Sales')
>>> for  e in (e1,e2,e3):
	if e.dept == m.dept:

>>> print m
Mike at Sales department is manager for An Employee called Eric working at Sales department,An Employee called Emma working at Sales department

Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65  mailto:magnus at

More information about the Tutor mailing list