4

I am wondering if we can use "import as" for creating relatively compact or readable code. I am aware of its usual use cases based on PEP such as to void name clashes.

Here is the situation (keeping it very simple for the demonstration purpose). Let's say I have a module name, process_words.py.

process_words.py:

def word_to_lower(word):
    return word.lower

process_article.py (lets the main script):

import process_words

word = "HELLO"
word_lower = process_words.word_to_lower(word)
print(word_lower)

Now would it be a bad or good practice to do something like this?:

import process_words as pw

word = "HELLO"
word_lower = pw.word_to_lower(word)
print(word_lower)

It's a very simplistic example. I have a couple of modules with each module having a couple of functions. Importing each function with from module import something isn't an option. To me, it feels like, if I use import as with some shortcut names, it will improve the code readability. Any thoughts?

NOTE: I am referring to custom modules here.

9
  • Personally I don't like to rename import with as. But sometimes it is required, for example if you want to import two objects with the same name into one namespace. Commented Oct 18, 2017 at 9:19
  • 1
    Use your best judgement. If you have something that’s used extremely often, then making a short alias can help (for example, it’s common to import sqlalchemy as sa when working with SQLAlchemy), but if you have too many aliases, then they can get hard to remember. And someone reading for the first time will have to look them up. Commented Oct 18, 2017 at 9:19
  • @KlausD. Yes, that's the usual use cases to avoid name clash. However, I am interested in using it for better readability which is kind of subjective though. Commented Oct 18, 2017 at 9:20
  • @Ryan I have very limited modules (2-5) usually so the alias will be limited too as I am importing modules than specific functions from them based in my use case. Commented Oct 18, 2017 at 9:21
  • 1
    There are technical reasons as to why you're able to import aliased, as already mentioned. i'm afraid anything regarding "better readability" might be subjective and can't be answered here Commented Oct 18, 2017 at 9:21

2 Answers 2

7

One practical issue is where you start changing libraries or modules used, and where you can switch the actual import without changing the code: import somemodule would become import newmodule as somemodule.

It's something I do within a try-except block when having Python 2/3 compatibility code (I guess a package like six has quite a bit of this). For example:

try:
    # Are we using Python 2?
    import StringIO as io
except ImportError:
    # No, we're using Python 3 instead
    import io
Sign up to request clarification or add additional context in comments.

2 Comments

are you referring to a use-case for import as? I assume you want to say this import as can be such in this specific situation for switching the actual import and similarly I can also use it for aliases in my use-case. If so then it would be nice if you can clarify it a bit in relationship to my question so I can accept it as an answer.
Another thing in python 2/3 is the urllib.parse etc. functions (they are the same but there location is different). I've also seen this with optional dependencies e.g. in the except block do io = None... not sure how I feel about it though.
4

Generally this should be avoided as much as possible.

However it is used to great effect when the import name is ubiquitous, for example numpy is always np, pandas is always pd. You shouldn't have to look up the name, you sit in front of an unseen code base: you see np.array you know what's going on. Although it's only slightly shorter, it can be skipped over much more easily.

Another time where it may be useful is in a single module which uses another module everywhere, every few lines or ten lines is a call-out to this module. Then I would consider using an as import to cut down the size (for reading) of the file.
I've done this before with a flask app that was a very thin wrapper around a (testable) module.

If it's an internal API only (and the meaning is completely clear from the context), then perhaps it makes sense to name it pw.py rather than process_words.py. IMO it's a little on the short side, but words.py might work well (however "process" is a bit of a generic word, so I might look foor a more specific verb...).
Context is important, as a submodule of words, it might make sense to call it funcs for example.

Comments

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.