PyCharm 2017.2 Help

Overriding Methods of a Superclass

Overview

You can override any method of a parent class, using the code generation facility. PyCharm creates a stub that contains a call to the method of the superclass, leaving the developer with the task of providing some meaningful source code.

Overriding methods

To override methods, follow these steps:

  1. With the class in question having the focus, invoke the Override method command in one of the following ways:
    • Press Ctrl+O.
    • On the main menu, choose Code | Override method.
    • Right-click the editor, choose Generate on the context menu, or press Alt+Insert, and choose Override methods.
  2. Select methods that can be overridden from the Select methods to override dialog box. The list of methods does not include the methods that are already overridden, or cannot be accessed from the current subclass.
    /help/img/idea/2017.2/py_overrideMethod.png
  3. Select one or more methods to override.
  4. Having generated the overriding method, create the required source code. Note the /help/img/idea/2017.2/gutterIconOverriding.png icon that marks the overriding method in the left gutter. Use this icon to view the name of the base class, and navigate to the overridden method.
    /help/img/idea/2017.2/py_overrideMethod1.png

Changing method body

File template responsible for overriding a method (Overridden method body) accepts predefined template variables from "File Header" (File | Settings - Editor - File and Code Templates - Code - File Header ), e.g. ${USER}, ${DATE}, etc.

For example, consider the following file template:

Last modified: 26 October 2017

See Also