Forum Archive

Quick Question on importing..

stephen

just my curiosity...

Would there be any benefit from "adopting" an import from another import?

Example


import scene

from scene import ui as gui

class MyView(gui.View):
    ...

ccc
>>> from scene import *
>>> ui.ACTIVITY_INDICATOR_STYLE_WHITE_LARGE
0
>>> ui.ACTIVITY_INDICATOR_STYLE_WHITE
1
>>> class MyView(ui.View):
    ...
stephen

@ccc

im just wondering if there is any benefit such as to overhead when doing this or if it makes no difference if i use, in this case, ui from scene or if i use scene and separate ui import?

ccc

Same thing either way...

>>> import ui
>>> id(ui)
4543879592
>>> from scene import ui as gui
>>> id(gui)
4543879592
>>> ui == gui
True
>>> ui is gui
True
stephen

10-4 thanks @ccc

JonB

I think where this is usually used is when the module has a long name, and that you use a lot within your code, so for readability you use an abbreviation

import numpy as np
from matplotlib import pyplot as plt
stephen

@JonB your statment on alias is correct 😃

But in this case we are speaking of Scene module importing ui and then Developer using ui module through scene with without making a separate import of ui in active script where __name__=="__main__"

Inside scene.py ...

from _scene2 import *
import _scene2

from scene_drawing import *

import math
from numbers import Number
from io import BytesIO
import ui

DEFAULT_ORIENTATION = 0
PORTRAIT = 1
LANDSCAPE = 2
...

inside myScript.py ...

from scene import ui

class MyView(ui.View):
    ...