Write a function that prints a class hierarchy. The function should take one argument that is an object of a class. The function should determine the class of that object and all direct and indirect base classes of the object. [Note: For simplicity, assume each class in the hierarchy uses only single inheritance.] The function prints each class name on a separate line. The first line contains

the top- most class in the hierarchy, and each level in the hierarchy is indented by three spaces. For example, the output for the function, when passed an object of class Cylinder from Fig. 9.8, should be:

What will be an ideal response?

```
# Function to print a class hierarchy.

def printHierarchy( argument ):
"""Prints an indented class hierarchy for an instance"""

classDict = {} # holds hierarchy levels

currentLevel = 0 # current hierarchy level
currentClass = argument.__class__ # current class in hierarchy

# add classes to hierarchy
while 1:
classDict[ currentLevel ] = currentClass.__name__
baseClasses = currentClass.__bases__

if not baseClasses:
break
else:
currentClass = baseClasses[ 0 ] # single inheritance

currentLevel += 1

# print hierarchy
spaceString = " " # printed to indent each level

# reverse levels to print top class first
levels = classDict.keys()
levels.sort()
levels.reverse()

for i in range( len( levels ) ):
print spaceString * i + classDict[ levels[ i ] ]
```
>>> cylinder = Cylinder( 10, 10, 10, 10 )
>>> printHierarchy( cylinder )
Point
Circle
Cylinder
>>>

Computer Science & Information Technology

You might also like to view...

A(n) ________ is made for very specific tasks that are usually predefined and require little or no input from the user

A) embedded computer B) supercomputer C) server D) mainframe computer

Computer Science & Information Technology

Your company has decided to use the SDLC to create and produce a new information system. You are training all users on how to protect company information while using the new system, along with being able to recognize social engineering attacks. Senior management must also formally approve of the system prior to it going live. In which of the following phases would these security controls take

place? A. Operations and Maintenance B. Initiation C. Acquisition and Development D. Implementation

Computer Science & Information Technology