PyCharm 2016.3 Help

Part 2. Navigating to a Declaration or Implementation

On this page:

Preparing an example

Do the following:

  1. Add package Animals under your project root (Alt+Insert | Python Package).
  2. Create the following Python files (Alt+Insert | Python File):
    • Mammals.py
    • Carnivore.py
    • Herbivore.py
  3. Open these files for editing (F4) and add the following code:
    • Mammals.py:
      from Animals.Carnivore import Carnivore from Animals.Herbivore import Herbivore class Mammalia(object): extremities = 4 def feeds(self): print ("milk") def proliferates(self): pass class Marsupial(Mammalia): def proliferates(self): print("poach") class Eutherian(Mammalia): def proliferates(self): print("placenta") class TasmanianDevil(Marsupial, Carnivore): pass class Duckbill(Marsupial, Herbivore): pass class Cat(Carnivore, Eutherian): pass class Tiger(Eutherian,Carnivore): pass class Cow(Eutherian, Herbivore): pass Cat.feeds()
    • Carnivore.py:
      from Animals.Mammal import Mammalia class Carnivore(Mammalia): def food(self): print("meat") pass
    • Herbivore.py:
      from Animals.Mammal import Mammalia class Herbivore (Mammalia): def food(self): print("grass") pass

Navigating to declaration

Place the caret at the method feeds of the instance of the class Cat, and press Ctrl+B. PyCharm jumps to the declaration of the method feeds, which is declared in the class Mammalia:

/help/img/idea/2016.3/py_goto_declaration.png

Navigating to implementation

Now, place caret at the declaration of the class Mammalia and try to find out which other classes implement it. To do that, press Ctrl+Alt+B. You see a rather long list of classes implementing Mammalia:

/help/img/idea/2016.3/py_goto_implementation.png

Ok, choose whichever implementation you need (for example, Cow ), and press Enter. PyCharm navigates to the selected implementation and places the caret at the class Cow declaration:

/help/img/idea/2016.3/py_goto_implementation_1.png

Should you select, for example Carnivore, which resides in a separate file, this file would open in a separate editor tab.

Side note about pin

Presumably, you have already noticed the pin icon /help/img/idea/2016.3/pin_small.png in the upper-right corner of the pop-up window. The same icon appears, for example, in the quick documentation lookup (Ctrl+Q). If you click this pin, the whole pop-up window will be "pinned", which in the case of navigation and search means that all the encountered occurrences will be presented in the Find tool window.

Navigating with gutter icons

Finally, let's look at the left gutter. You see there a number of icons with the arrows pointing up or down. What does it mean?

If you hover your mouse pointer over an icon, PyCharm will show the list of child classes or overriding methods (in case of the down arrow), or the parent classes (in case of the up arrow):

/help/img/idea/2016.3/py_implementation_declaration.png

What happens, if you click an icon? If a certain class is subclassed, or a method is overridden in more than one class, PyCharm will suggest to select the desired target from the list:

/help/img/idea/2016.3/py_declaration_implementation_1.png

After that, PyCharm jumps to the selected target, and places the caret at the class (method) declaration. If there is only one superclass/subclass, or method, then such a navigation is done silently.

See Also

Last modified: 23 December 2016