views:

24

answers:

1

The descriptor protocol in Python 2.6 is only defined for class definitions, and thus can only be used by instances.

Is there some equivalent for instrumenting get/set of globals?

I'm trying to speed up the importing of a module which interacts with the host system, and as such has to perform some expensive probing of the host. The results of the (expensive) probe are stored in a module global that is initialized at import time; so I'm trying to delay the initialization until absolutely required.

Please, no comments about globals being evil. I know what they are and when to use them.

My current plan is to create a global instance that uses descriptors, and move all my current globals into the attributes of this instance. I expect this will work; I'm just asking if there's another way.

+1  A: 

My current plan is to create a global instance that uses descriptors, and move all my current globals into the attributes of this instance. I expect this will work; I'm just asking if there's another way.

That's precisely what I would do. There is no equivalent to descriptors outside of classes.

The other option, which I have also sometimes used, would be to use a function instead of a variable name, something like this:

_expensive_to_compute = None
def get_expensive_to_compute():
    global _expensive_to_compute
    if _expensive_to_compute is None:
        _expensive_to_compute = do_computation()
    return _expensive_to_compute

If you already have a @memoize decorator defined somewhere, you can simplify the above considerably.

Daniel Stutzbach
I suspected this would be the case. :) Thanks.
RobM