class - python import nested classes shorthand -
how import nested package using "as" shorthand?
this question similar importing module in nested packages nesting within same .py file, not across folders.
in foo.py (all python files in same package, , version 3.4):
class foo: class bar: ...
i can access these subclasses in .py file:
from . import foo ... bar = foo.foo.bar()
what do:
from . import foo.foo.bar bar # not work: "unresolved reference" error. ... bar = bar() # saves typing. bar2 = bar() ...
is there way this?
there little point in nesting python classes; there no special meaning attached doing other nesting namespaces. there rarely need so. use modules instead if need produce additional namespaces.
you cannot directly import nested class; can import module globals, foo
in case. you'd have import outer-most class , create new reference:
from .foo import foo bar = foo.bar del foo # remove imported foo class again module globals
the del foo
entirely optional. above illustrate why you'd not want nest classes begin with.
Comments
Post a Comment