python 模块 __init__ 函数
python module __init__ function
有什么方法可以为模块(不是包)创建隐式初始化器吗?
类似于:
#file: mymodule.py
def __init__(val):
global value
value = 5
当您导入它时:
#file: mainmodule.py
import mymodule(5)
import
语句使用 builtin __import__
function.
因此不可能有模块 __init__
函数。
你必须自己调用它:
import mymodule
mymodule.__init__(5)
这些东西通常不会作为重复项关闭,所以这里有一个来自 Pass Variable On Import 的非常好的解决方案。 TL;DR:使用配置模块,在导入模块之前对其进行配置。
[...] A cleaner way to do it which is very useful for multiple configuration
items in your project is to create a separate Configuration module
that is imported by your wrapping code first, and the items set at
runtime, before your functional module imports it. This pattern is
often used in other projects.
myconfig/__init__.py :
PATH_TO_R_SOURCE = '/default/R/source/path'
OTHER_CONFIG_ITEM = 'DEFAULT'
PI = 3.14
mymodule/__init__.py :
import myconfig
PATH_TO_R_SOURCE = myconfig.PATH_TO_R_SOURCE
robjects.r.source(PATH_TO_R_SOURCE, chdir = True) ## this takes time
class SomeClass:
def __init__(self, aCurve):
self._curve = aCurve
if myconfig.VERSION is not None:
version = myconfig.VERSION
else:
version = "UNDEFINED"
two_pi = myconfig.PI * 2
And you can change the behaviour of your module at runtime from the
wrapper:
run.py :
import myconfig
myconfig.PATH_TO_R_SOURCE = 'actual/path/to/R/source'
myconfig.PI = 3.14159
# we can even add a new configuration item that isn't present in the original myconfig:
myconfig.VERSION="1.0"
import mymodule
print "Mymodule.two_pi = %r" % mymodule.two_pi
print "Mymodule.version is %s" % mymodule.version
Output:
> Mymodule.two_pi = 6.28318
> Mymodule.version is 1.0
有什么方法可以为模块(不是包)创建隐式初始化器吗? 类似于:
#file: mymodule.py
def __init__(val):
global value
value = 5
当您导入它时:
#file: mainmodule.py
import mymodule(5)
import
语句使用 builtin __import__
function.
因此不可能有模块 __init__
函数。
你必须自己调用它:
import mymodule
mymodule.__init__(5)
这些东西通常不会作为重复项关闭,所以这里有一个来自 Pass Variable On Import 的非常好的解决方案。 TL;DR:使用配置模块,在导入模块之前对其进行配置。
[...] A cleaner way to do it which is very useful for multiple configuration items in your project is to create a separate Configuration module that is imported by your wrapping code first, and the items set at runtime, before your functional module imports it. This pattern is often used in other projects.
myconfig/__init__.py :
PATH_TO_R_SOURCE = '/default/R/source/path' OTHER_CONFIG_ITEM = 'DEFAULT' PI = 3.14
mymodule/__init__.py :
import myconfig PATH_TO_R_SOURCE = myconfig.PATH_TO_R_SOURCE robjects.r.source(PATH_TO_R_SOURCE, chdir = True) ## this takes time class SomeClass: def __init__(self, aCurve): self._curve = aCurve if myconfig.VERSION is not None: version = myconfig.VERSION else: version = "UNDEFINED" two_pi = myconfig.PI * 2
And you can change the behaviour of your module at runtime from the wrapper:
run.py :
import myconfig myconfig.PATH_TO_R_SOURCE = 'actual/path/to/R/source' myconfig.PI = 3.14159 # we can even add a new configuration item that isn't present in the original myconfig: myconfig.VERSION="1.0" import mymodule print "Mymodule.two_pi = %r" % mymodule.two_pi print "Mymodule.version is %s" % mymodule.version
Output:
> Mymodule.two_pi = 6.28318 > Mymodule.version is 1.0