0

from python docs:

 import_stmt:    "import" module ["as" name] ("," module ["as" name] )* 
          | "from" module "import" identifier ["as" name]
            ("," identifier ["as" name] )*
          | "from" module "import" "*" 
 module:         (identifier ".")* identifier

Why would we need to define an alias for a library?

10
  • 1
    What if you don't like the name it gives you? Commented Oct 19, 2018 at 2:04
  • What if you had two (or more) conflicting modules? What if you just want to save yourself some typing? Commented Oct 19, 2018 at 2:07
  • 2
    You don't need to. It's an option. Commented Oct 19, 2018 at 2:07
  • 2
    All three above comments constitute a complete answer to this question, in my opinion. Commented Oct 19, 2018 at 2:08
  • I think it's pretty darn common in most languages that have an import system to allow aliasing…!? Commented Oct 19, 2018 at 2:10

1 Answer 1

5

There are a few occasions.

To avoid name collision

from bisect import bisect
from homework import bisect as my_bisect
# TODO: test if the two functions works similarly

To avoid long names

import tensorflow as tf

To make memes

import keras as tf
Sign up to request clarification or add additional context in comments.

1 Comment

import jackie_chan from drawings as what_is_this_I_cant_even # made a meme

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.