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.
as. But sometimes it is required, for example if you want to import two objects with the same name into one namespace.import sqlalchemy as sawhen 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.