Creating a subclass

To create a class that inherits from another class, you type class, the name of the subclass, and then, in parentheses, the name of the superclass the subclass inherits from. To create a subclass of the NameTag class we work with earlier, you use a class statement like this:

class GeekNameTag(NameTag):

Tip Python 2.5 adds a new feature: classes without base classes can use empty parentheses (the same way functions can have no arguments). That is, you don't have to specify a superclass in the parentheses. For example, you could just type class C():

In general, when you create a subclass, you create only what you want to override or extend about the superclass's behavior. So you don't need to write an_init_()

statement unless you want to make changes to the superclass's_init_() statement.

Was this article helpful?

0 0

Post a comment