Replace a module variable with a function call

  • Thread starter gabriel.becedillas
  • Start date
G

gabriel.becedillas

I have a module that defines a variable with a constant value and now I
need to make that value dynamic, without affecting module clients. In
other words, I need to call a function witout using parenthesis.
Example:

mymod.py----------------------

def value():
return "hi"

client.py--------------------------
import mymod

print mymod.value

Is there a way to do this ?
Thanks.
 
C

Carsten Haese

I have a module that defines a variable with a constant value and now I
need to make that value dynamic, without affecting module clients. In
other words, I need to call a function witout using parenthesis.
Example:

mymod.py----------------------

def value():
return "hi"

client.py--------------------------
import mymod

print mymod.value

Is there a way to do this ?

If you're not passing any arguments to this "function call", what is
this "dynamic" value supposed to depend on? The phase of the moon?

-Carsten
 
G

gabriel.becedillas

There are modules (like os) that define some stuff that depends on the
platform (for example linesep). This platform dependent constants gets
their values assigned when the module gets loaded, but in our
application, different threads might run on different computers (by
proxying syscalls).. and we need to calculate that value every time the
constant gets referenced. We have a slightly modified version of
python's distro to accomplish this.

It sort of a design problem, like not exposing member variables of a
class to avoid breaking clients, and use an accesor function instead.

So.. it doesn't depend on the phase of the moon.. it depends on the
architecture of a host.
I can write ten million examples of functions that doesn't depend on
arguments to calculate a dynamic value, but since you couldn't figure
that out by yourself.. I'm not going to waste my time with more
examples coz probably you won't understand them either.
 
T

Tim Hochberg

I have a module that defines a variable with a constant value and now I
need to make that value dynamic, without affecting module clients. In
other words, I need to call a function witout using parenthesis.
Example:

mymod.py----------------------

def value():
return "hi"

client.py--------------------------
import mymod

print mymod.value

Is there a way to do this ?


It can be done, but you're not really supposed to be doing this, so it's
moderately horrible to do so. If you can manage, I'd avoid doing this,
but if you have to, the strategy is to (1) move the contents of your
module into a class, (2) use properties to convert what is now a method
call into an attribute access and (3) replace the module in sys.modules
with an instance of this class. This occasionally get you into trouble,
for instance reload no longer works, but it has mostly worked in my
limited experience.

Since I'm sure that's pretty opaque, here's an example:

# file noparens.py
import sys

class Module(object):

def get_value(self):
return "hi"
value = property(get_value)

sys.modules[__name__] = Module()

# interpreter session'hi'

regards,

-tim
 
C

Carsten Haese

I'm not going to waste my time with more
examples coz probably you won't understand them either.

Fine, I won't waste my time trying to help you.

-Carsten.
 
G

gabriel.becedillas

Your post didn't provide any help at all, it was a useless sarcastic
post and I'm a very sensible person.
 
C

Carsten Haese

Your post didn't provide any help at all, it was a useless sarcastic
post and I'm a very sensible person.

Your original question didn't provide enough detail to offer an answer,
which is why I asked the question what the dynamic return value should
depend on. I am sorry if my conjecture that the return value might
depend on the phase of the moon offended you, but my post was not meant
to be useless. It was a necessary follow-up question because you didn't
define your requirements clearly enough.

If the condition upon which the return value is determined can be
evaluated at import time and doesn't change henceforth, a simple if
statement in the module will serve your need.

If the value *really* needs to be re-evaluated every time the name is
looked up, you'll need to follow the advice that Tim Hochberg has
already given on this thread. Note, though, that that won't prevent the
user code from caching the return value and circumventing the
re-evaluation.

-Carsten
 
G

gabriel.becedillas

Tim and Carsten,
Thank you very much for your replies. I'm afraid this is not going to
work for me (but I'm not 100% sure), coz if I access those modules from
the Python's C API (PyModule_* functions), the PyModule_Check() calls
will fail.
Thanks again.
 

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,794
Messages
2,569,641
Members
45,354
Latest member
OrenKrause

Latest Threads

Top