I have a python package
- my_package
- __init__.py
# inside
def getSingleton() # gets an instance of an object in the package
- AClass.py
# inside
class AClass:
...
def instanceMathod(self,arg):
singleton = (parent package).getSingleton()
I want to use the module "static" functions from within class instances from the module ie: reference the parent module from a contained class instance? Can one do this? Is there a better way? Much works well as the singleton will be destroyed when the containing module is destroyed when all importing modules have been destroyed. I'm using python3
Use:
def instanceMethod(self,arg):
from . import getSingleton
singleton = getSingleton()
Here we are deferring the import until the method is called, rather than importing at global scope, in case mypackage.__init__
is also importing AClass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.