Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Variable On Import

Tags:

python

import

Let's say you have some time-consuming work to do when a module/class is first imported. This functionality is dependent on a passed in variable. It only needs to be done when the module/class is loaded. All instances of the class can then use the result.

For instance, I'm using rpy2:

import rpy2.robjects as robjects  PATH_TO_R_SOURCE = ## I need to pass this robjects.r.source(PATH_TO_R_SOURCE, chdir = True) ## this takes time  class SomeClass:   def __init__(self, aCurve):     self._curve = aCurve    def processCurve(self):     robjects.r['someRFunc'](robjects.FloatVector(self._curve)) 

Am I stuck creating a module level function that I call to do the work?

import someClass someClass.sourceRStuff(PATH_TO_R_SOURCE) x = someClass.SomeClass([1,2,3,4]) etc... 
like image 703
Mark Avatar asked Sep 15 '10 18:09

Mark


People also ask

Can you import variables in Python?

Python's from statement lets you import specific attributes from a module. from <file_name> import * and then use variables directly.

How do you pass parameters to a Python module?

There is no such way to pass parameters to the module, however you can revamp your code a bit and import the parameters from a module as global parameters.

What is import variable?

User variables are specific to the SEER*Stat database for which they were created, but their definitions can be imported into other databases. Your process for doing so may differ depending on whether you have access to the original database.

What is __ import __ in Python?

__import__() Parameters name - the name of the module you want to import. globals and locals - determines how to interpret name. fromlist - objects or submodules that should be imported by name. level - specifies whether to use absolute or relative imports.


2 Answers

Having a module init function isn't unheard of. Pygame does it for the sdl initialization functions. So yes, your best bet is probably

import someModule someModule.init(NECESSARY_DATA) x = someModule.someClass(range(1, 5)) 
like image 133
nmichaels Avatar answered Sep 21 '22 05:09

nmichaels


I had to do something similar for my project. If you don't want to rely on the calling script to run the initialization function, you can add your own Python builtin which is then available to all modules at runtime.

Be careful to name your builtin something unique that is unlikely to cause a namespace collision (eg myapp_myvarname).

run.py

import __builtin__ __builtin__.myapp_PATH_TO_R_SOURCE = 'path.to.r.source' import someClass 

someClass module .py

import rpy2.robjects as robjects import __builtin__  if hasattr(__builtin__, "myapp_PATH_TO_R_SOURCE"):     PATH_TO_R_SOURCE = __builtin__.myapp_PATH_TO_R_SOURCE else:     PATH_TO_R_SOURCE = ## Some default value or Null for Exception handling robjects.r.source(PATH_TO_R_SOURCE, chdir = True)  ... 

This works well for variables that may have a default but you want to allow overriding at import time. If the __builtin__ variable is not set, it will use a default value.

Edit: Some consider this an example of "Monkey patching". For a more elegant solution without monkey patch, see my other answer.

like image 34
Aaron D Avatar answered Sep 22 '22 05:09

Aaron D