[Tutor] Importing from classes
Steven D'Aprano
steve at pearwood.info
Wed Feb 11 11:18:17 CET 2015
On Tue, Feb 10, 2015 at 06:39:50PM -0800, daaku gee wrote:
> Hi
>
> When importing from other modules, I've seen syntax like:
>
> import from <module> <Class>
> import <module>
>
> And another one:
> import from <module> <Class> as <some_name>
Not quite. The syntax is:
import <module>
import <module> as <new name>
from <module> import <object> # not necessarily a class
from <module> import <object> as <new name>
> Is one better than the other or is it just personal choice?
As far as Python is concerned, there is no difference at all. The "as
name" version just lets you pick a different name, usually to save
typing:
import module_with_a_really_long_name as module
Obviously the usual rule for naming things applies here too: names
should be meaningful, they should not lie, or be confusing:
# Bad ideas.
import math as string # What?
import string as fred
import os as barney
Beware of names which are easily confused, or have common meanings:
- try to avoid `l` and `I` because in some fonts they look like 1
- same for `O` and 0
- i is normally used for for-loop counters and other integers
- n is also used for integers
- x and y for floats
But apart from that, the names you choose are entirely up to you.
--
Steve
More information about the Tutor
mailing list