On 06/17/2012 11:35 PM, Gelonida N wrote:
Hi,
I'm not sure whether what I ask for is impossible, but would know how
others handle such situations.
I'm having a module, which should lazily evaluate one of it's variables.
Meaning that it is evaluated only if anybody tries to use this variable.
At the moment I don't know how to do this and do therefore following:
####### mymodule.py #######
var = None
def get_var():
global var
if var is not None:
return var
var = something_time_consuming()
Now the importing code would look like
import mymodule
def f():
var = mymodule.get_var()
The disadvantage is, that I had to change existing code directly
accessing the variable.
I wondered if there were any way to change mymodule.py such, that the
importing code could just access a variable and the lazy evaluation
would happen transparently.
import mymodule
def f():
var = mymodule.var
Thanks everybody for your responses. This gave me quite some ideas.
It seems, that none of the solutions would allow to have the changes
only in the module.
More out of curiosity than out of real necessity I wanted to know,
whether it would be possible to hide the lazy evaluation from already
existing code, that has already the import statement written.
So the question basically boiled down to
"can one make 'properties' for modules?"
It seems no.
Probably there aren't many real use cases except for monkey patching
libraries or for logging accesses to a module's variable
--
http://mail.python.org/mailman/listinfo/python-list