How to import multiple classes/objects and rename some of them in one line in Python?

Example

from mymodule import A as A1, B, C

In the above example, A is renamed as A1, B and C are not changed.

Why do we want to rename the class/object?

There are several reasons:

  • To shorten the names, like numpy to np, pandas to pd
  • There might be conflict in naming, say we might have module1.A and module2.A, then we can rename one of them
  • Personal preference, somethings we just want it that way, however it helps us writing the code