python - Python3: How to provide module exports as convenience at the package level? -


hopefully question won't me python hall of shame.

anyway, have package poo contains single module moo. in future, there might additional modules inside poo (such poo.moose). let's assume moo kind of main-use module , users want have things in moo ready @ hand when import poo, package. (and package users) avoid import poo.moo, import poo.moo moo, , (gasp!) import poo.*. know, saving typing thing.

so idea (which not off right way) copy "exports" (functions, variables, classes, ...) of module poo.moo package poo, in __init.py__. don't want overwrite existing definitions in poo though.

what correct, good, proper, , efficient way achieve this? (did use "ideal"? nah!) or bad idea @ all? or highly spirited python in sense of "...and different"? ;)

oh, packages python3 only.

please note in python, how import classes module without keeping imported module's namespace? has answer, did not understand how apply __init__.py code of python package. reason, detailed answer below gives such additional details.

turns out easy. in package poo/__init__.py (absolute) import of module package namespace:

from poo.moo import * 

that's it. when there's poo.moo.a() become accessible poo.a() too.

please note package definitions aren't overwritten (sub) module import, intention: poo.moo.version=42 doesn't overwrite poo.version=13. needed.

i notice i'm still addled other modularized languages imports stay private module. in python in plain view, package imports in module/package.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -