lazy evaluation of a variable

G

Gelonida N

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 in advance for you suggestions
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top