__All__ variables defined by the __init__.py to simplify writing from * import *

 Below is a belt structure is introduced into the package used, the package name is long, from * import * to write a lot of trouble

To solve the problems, we wrote the following in __init__.py

1 from .httputil import HTTPUtil
2 from .jsonutil import JSONUtil
3 from .redisutil import RedisUtil
4 from .time import TimeUtil
5 from .randomutil import myrandom
6 
7 __all__=["HTTPUtil","JSONUtil","RedisUtil","TimeUtil","myrandom"]

 

After the package is installed, you can use the following introduction manner, function or method used inside the package

1 from ifacetools import myrandom,TimeUtil
2 
3 print(myrandom.uuidWithHyphen())
4 tu=TimeUtil.TimeUtil()
5 print(tu.getTargetDays(-1,"%Y-%m-%d"))

 

 

If no __init __. Py prepared in the above content (at ifacetools package)

Then you must use the following. Would be more trouble, if there are many module need to be introduced, it will bring trouble to read the writing.

from ifacetools.randomutil import myrandom
from ifacetools.time import TimeUtil

print(myrandom.uuidWithHyphen())
tu=TimeUtil.TimeUtil()
print(tu.getTargetDays(-1,"%Y-%m-%d"))

 

Guess you like

Origin www.cnblogs.com/moonpool/p/11333117.html