passing global data to a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • beliavsky@aol.com

    passing global data to a function

    How can I pass global data to function stored in a separate file?
    For example, in file "funk.py" is the code

    ipow = 2
    def xpow(xx): return xx**ipow

    and in "xfunk.py" is the code

    from funk import xpow,ipow
    xx = 4.0
    print xpow(xx)
    ipow = 3
    print xpow(xx)

    Running Python against xfunk.py, I get the output

    16.0
    16.0,

    so the value of ipow in function xpow is not being changed. I want ipow to equal
    4 in the 2nd call to xpow, so that the output would be

    16.0
    64.0

    How can this be done? Thanks.
  • Hans Nowak

    #2
    Re: passing global data to a function

    beliavsky@aol.c om wrote:[color=blue]
    > How can I pass global data to function stored in a separate file?
    > For example, in file "funk.py" is the code
    >
    > ipow = 2
    > def xpow(xx): return xx**ipow
    >
    > and in "xfunk.py" is the code
    >
    > from funk import xpow,ipow
    > xx = 4.0
    > print xpow(xx)
    > ipow = 3
    > print xpow(xx)[/color]

    Try:

    import funk

    xx = 4.0
    print funk.xpow(xx)
    funk.ipow = 3
    print funk.xpow(xx)

    Of course, there might be a better way to do this than with globals... a class
    comes to mind as one possible alternative.

    HTH,

    --
    Hans (hans@zephyrfal con.org)
    Memimpin Angin Perubahan Teknologi




    Comment

    • Bengt Richter

      #3
      Re: passing global data to a function

      On 1 Dec 2003 08:55:38 -0800, beliavsky@aol.c om wrote:
      [color=blue]
      >How can I pass global data to function stored in a separate file?
      >For example, in file "funk.py" is the code
      >
      >ipow = 2
      >def xpow(xx): return xx**ipow
      >
      >and in "xfunk.py" is the code
      >
      >from funk import xpow,ipow
      >xx = 4.0
      >print xpow(xx)
      >ipow = 3
      >print xpow(xx)
      >
      >Running Python against xfunk.py, I get the output
      >
      >16.0
      >16.0,
      >
      >so the value of ipow in function xpow is not being changed. I want ipow to equal
      >4 in the 2nd call to xpow, so that the output would be
      >
      >16.0
      >64.0
      >
      >How can this be done? Thanks.[/color]

      Not nice to pass args via globals, but
      (oops, extra blank lines at end of funk.py):
      [color=blue][color=green][color=darkred]
      >>> print '----\n%s\n----'%file('funk.py ').read()[/color][/color][/color]
      ----
      ipow = 2
      def xpow(xx): return xx**ipow


      ----[color=blue][color=green][color=darkred]
      >>> import funk
      >>> xpow = funk.xpow
      >>> xx = 4.0
      >>> print xpow(xx)[/color][/color][/color]
      16.0[color=blue][color=green][color=darkred]
      >>> funk.ipow = 3
      >>> print xpow(xx)[/color][/color][/color]
      64.0

      IOW, if you use
      from funk import xpow, ipow
      you get local bindings of the names xpow and ipow
      and when you call xpow(xx) you are accessing funk.xpow,
      but when you assign ipow = 3 you are just changing the local
      binding of ipow from what it was (same as funk.ipow) to a new value 3,
      which doesn't affect funk.ipow, which is what xpow is using.
      So you have to rebind the ipow name in xpow's global name space,
      hence funk.ipow = 3 does what you wanted.

      But you probably ought to find a better way to design this functionality.

      Regards,
      Bengt Richter

      Comment

      • Terry Reedy

        #4
        Re: passing global data to a function


        <beliavsky@aol. com> wrote in message
        news:3064b51d.0 312010855.66f4e 2@posting.googl e.com...[color=blue]
        > How can I pass global data to function stored in a separate file?[/color]

        Since 'global data' (and more generally 'context data') is data that
        is *not* passed to a function (but is instead read from the function's
        context), your question is a contradiction;-). What you have run into
        is the difference between lexical context (where the function is
        defined) and dynamic context (where the function is called). Dynamic
        scoping, which you were expecting or at least hoping for has been
        tried in some languages (some dialects of Lisp, at least) but I
        believe that experience has shown lexical scoping is overall
        preferable.

        As others have said, your immediate solution is to push the 'global'
        value into the functions lexical context with 'funk.ipow = 3'.

        Terry J. Reedy


        Comment

        • David MacQuigg

          #5
          Re: passing global data to a function

          On 1 Dec 2003 08:55:38 -0800, beliavsky@aol.c om wrote:
          [color=blue]
          >How can I pass global data to function stored in a separate file?
          >For example, in file "funk.py" is the code
          >
          >ipow = 2
          >def xpow(xx): return xx**ipow
          >
          >and in "xfunk.py" is the code
          >
          >from funk import xpow,ipow
          >xx = 4.0
          >print xpow(xx)
          >ipow = 3
          >print xpow(xx)
          >
          >Running Python against xfunk.py, I get the output
          >
          >16.0
          >16.0,
          >
          >so the value of ipow in function xpow is not being changed. I want ipow to equal
          >4 in the 2nd call to xpow, so that the output would be
          >
          >16.0
          >64.0[/color]

          I found the scoping rules confusing also. In case anyone is writing a
          text, here is a simpler example:

          def fonc(): return p
          # from funk import fonc # same def, but in module funk.py
          # import funk
          p = 2; print fonc()
          p = 3; print fonc()
          ### RESULTS from uncommenting line #N, where #N =
          #1) As expected.
          #2) 'p' is not defined.
          #3) 'fonc' is not defined

          The variable 'p' trickles down ONLY to definitions within the same
          module. ( "Lexical scoping", as explained by Terry Reedy. ) I'm not
          sure why this is the right thing to do, but after a few months of
          working with Python, my confidence is still growing that the designers
          made the right choices. This is a superb language!! Perfect for a
          technical professional like myself who is not a programmer.

          Anyway, I have no complaint about this limitation. I agree with Bengt
          Richter, this "back door" is not a good way to pass parameters into a
          function.

          Now if only we could simplify the scoping rules by having global
          definitions pass down through nested classes the same as they do
          through nested functions ... :>)

          -- Dave


          Comment

          • Hung Jung Lu

            #6
            Re: passing global data to a function

            beliavsky@aol.c om wrote in message news:<3064b51d. 0312010855.66f4 e2@posting.goog le.com>...[color=blue]
            > How can I pass global data to function stored in a separate file?
            > For example, in file "funk.py" is the code
            >
            > ipow = 2
            > def xpow(xx): return xx**ipow
            >
            > and in "xfunk.py" is the code
            >
            > from funk import xpow,ipow
            > xx = 4.0
            > print xpow(xx)
            > ipow = 3
            > print xpow(xx)[/color]

            I am a bit tired of the standard answer, so let us try a funkier
            approach! :)

            # funk.py
            ipow = 2
            def xpow(xx): return xx**ipow

            # xfunk.py
            import inspect, funk
            exec inspect.getsour ce(funk)
            xx = 4.0
            print xpow(xx)
            ipow = 3
            print xpow(xx)

            And if you want trace information and be able to step through in a
            debugger, you can use:

            # funk.py
            ipow = 2
            def xpow(xx):
            xx = xx / 0
            return xx**ipow

            # xfunk.py
            import inspect, funk
            exec compile(inspect .getsource(funk ), inspect.getabsf ile(funk),
            'exec')
            xx = 4.0
            print xpow(xx)

            regards,

            Hung Jung

            Comment

            Working...