• No results found

Subclasses and Inheritance

In document Making Use of Python (Page 191-193)

Inheritance is the property by which a subclass derives the attributes of the base class. The term “subclass” describes a class that inherits or derives the attributes from another class; the term “base class” describes a class from which a subclass has been derived. To understand this better, let’s relate this to the concept of parent and child. In inheritance, the base class is also termed as the parent and a subclass as the child. A child class can be a parent class for some other classes and so on. All classes higher than the parent class are termed as ancestors. This cycle of derivation can continue for multiple levels. This provides the benefit of code reusability.

The subclasses inherit most of the attributes of their base classes. Thus, a subclass has more attributes than its base class. A subclass can also modify some or all of the inherited attributes of the base class, but the base class cannot do anything with the attributes of the subclass. The syntax for declaring a subclass looks like this:

class Sub_Class_Name(Base_Class1[, Base_Class2, ...]): ‘class_docstring’

class_suite

It is similar to the syntax used for declaring a base class, the only difference being that a list of all the base classes of the subclass is provided after the name of the subclass. Let’s look at the following example to explain the concept of base classes and subclasses.

>>>class My_Base_Class:

... ‘My_Base_Class is the parent class of My_Subclass’ ... def my_base_class_method(self):

... return ‘Base class method’ ...

>>>class My_Subclass(My_Base_Class):

... ‘My_Subclass is the child class of My_Base_Class’ ... def my_subclass_method(self):

... return ‘Subclass method’ ...

>>>

>>>b = My_Base_Class() >>>

>>>dir(b)

[‘__doc__’, ‘__module__’, ‘my_base_class_method’] >>>

>>>

>>>s = My_Subclass() >>>

>>>dir(s)

[‘__doc__’, ‘__module__’, ‘my_base_class_method’, ‘my_subclass_method’] >>>

>>>s.my_base_class_method() ‘Base class method’

>>>

>>>b.my_subclass_method()

Traceback (most recent call last): File “<pyshell#17>”, line 1, in ?

b.my_subclass_method()

AttributeError: My_Base_Class instance has no attribute ‘my_subclass_method’

In this example, two classes, My_Base_Class and My_Subclass, are defined. My_ Base_Classis the parent class of My_Subclass. It defines one method, my_base_ class_method. My_Subclass also defines one method, my_subclass_method.

The dir() built-in function is used to list the attributes of the class. The output of My_Base_Classshows my_base_class_method, but the output of My_Subclass shows both my_base_class_method and my_subclass_method. This shows that My_Subclass, which is the child class of My_Base Class, has inherited the attrib- utes of the parent class.

As My_Subclass is a subclass of My_Base_Class, the my_base_class_method can be invoked by using b, which is an instance of the subclass, My_Subclass. But, when the my_subclass_method is invoked by using s, which is an instance of the base class, My_Base_Class, it results in an error. This shows that inheritance is only one-way; hence, only a subclass inherits the attributes of the base class and not vice versa.

Multiple Inheritance

When a class has one or multiple subclasses, it is known as inheritance. When a sub- class is inherited from multiple base classes, it is known as multiple inheritance. All the subclasses derived from a single base class are termed as siblings.

When a class is derived from multiple classes, it is sometimes difficult to figure out the parent classes. Python provides you with a class attribute, __bases__ that you can use to show the set of base classes for a subclass. __bases__ is a tuple and dis- plays only the parent class of a subclass and not all the ancestor classes.

Let’s consider an example to explain multiple inheritance and the use of the __bases__attribute. This would require using the classes created earlier and defining two new classes, My_Class_A and My_Class_B. My_Class_A will be the subclass of the class, My_Subclass. My_Class_B will be the subclass of the classes, My_Class_Aand My_Base_Class.

>>>class My_Class_A(My_Subclass):

... ‘My_Class_A is the child class of My_Subclass’ ... def my_subclass_method(self):

... return ‘Method of My_Class_A’ ...

>>>My_Class_A.__bases__

(<class __main__.My_Subclass at 0x009462EC>,) >>>

>>>class My_Class_B(My_Class_A, My_Base_Class):

... ‘My_Class_B is the child class of My_Class_A and My_Base_Class’ ... def my_subclass_method(self):

... return ‘Method of My_Class_B’ ...

>>>My_Class_B.__bases__

(<class __main__.My_Class_A at 0x00955B84>, <class __main__.My_Base_Class at 0x00936344>)

Even if My_Class_A is derived from My_Subclass, the result of My_Class_B.__ bases__shows only the two parent classes, My_Class_A and My_Base_Class.

Result

The library class will be the base class, and both the books and software classes will be the subclasses of the Library class. The library class will define the attrib- utes and methods that are common to both the books and software items, and it can be used by both the books and software classes. Both these classes will inherit all the objects of the Library class.

class library: ‘library class’ ... ... ... class books(library): ‘books class’ ... ... ... class software(library): ‘software class’ ... ... ...

In document Making Use of Python (Page 191-193)