Replace a module variable with a function call

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gabriel.becedillas@gmail.com

    #1

    Replace a module variable with a function call

    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.

  • Carsten Haese

    #2
    Re: Replace a module variable with a function call

    On Mon, 2006-02-06 at 13:54, gabriel.becedil las@gmail.com wrote:[color=blue]
    > 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 ?[/color]

    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


    Comment

    • gabriel.becedillas@gmail.com

      #3
      Re: Replace a module variable with a function call

      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.

      Comment

      • Tim Hochberg

        #4
        Re: Replace a module variable with a function call

        gabriel.becedil las@gmail.com wrote:[color=blue]
        > 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 ?[/color]


        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_va lue)

        sys.modules[__name__] = Module()

        # interpreter session[color=blue][color=green][color=darkred]
        >>> import noparens
        >>> noparens.value[/color][/color][/color]
        'hi'

        regards,

        -tim

        Comment

        • Carsten Haese

          #5
          Re: Replace a module variable with a function call

          On Mon, 2006-02-06 at 14:19, gabriel.becedil las@gmail.com wrote:[color=blue]
          > I'm not going to waste my time with more
          > examples coz probably you won't understand them either.[/color]

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

          -Carsten.


          Comment

          • gabriel.becedillas@gmail.com

            #6
            Re: Replace a module variable with a function call

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

            Comment

            • Carsten Haese

              #7
              Re: Replace a module variable with a function call

              On Mon, 2006-02-06 at 14:38, gabriel.becedil las@gmail.com wrote:[color=blue]
              > Your post didn't provide any help at all, it was a useless sarcastic
              > post and I'm a very sensible person.[/color]

              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


              Comment

              • gabriel.becedillas@gmail.com

                #8
                Re: Replace a module variable with a function call

                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.

                Comment

                Working...