access to the namespace of a function from within its invocation

Discussion in 'Python' started by Poor Yorick, Jul 13, 2007.

  1. Poor Yorick

    Poor Yorick Guest

    In the example below, the attribute "data" is added to a function
    object. "me" can be used to get the function when it is invoked using
    an identifier that matches the "co_name" attribute of function's code
    object. Can anyone conjure an example of accessing fun2.data from
    without prior knowledge of the value of fun2.f_code.co_name?

    ###code begin###
    #!/bin/python

    import sys

    def me():
    t = sys._getframe(0)
    return t.f_back.f_globals[t.f_back.f_code.co_name]
    def fun1():
    m = me
    print me().data

    def makefun () :
    def tmpfunc():
    print 'need something like me().data'
    return tmpfunc

    fun1.s = fun1
    fun1.data=['one', 'two', 'three']
    fun1()
    fun2 = makefun()
    fun2.data=['four', 'five','six']
    fun2()

    ###code end###

    --
    Poor Yorick
    Poor Yorick, Jul 13, 2007
    #1
    1. Advertising

  2. Poor Yorick

    John Nagle Guest

    Poor Yorick wrote:
    > In the example below, the attribute "data" is added to a function
    > object.


    There are these things called "classes" which might be useful
    in this situation.

    Python has some gratitious semantics that come with implementations
    in which everything is a dictionary. Don't get carried away.

    John Nagle
    John Nagle, Jul 13, 2007
    #2
    1. Advertising

  3. Poor Yorick a écrit :
    > In the example below, the attribute "data" is added to a function
    > object. "me" can be used to get the function when it is invoked using
    > an identifier that matches the "co_name" attribute of function's code
    > object. Can anyone conjure an example of accessing fun2.data from
    > without prior knowledge of the value of fun2.f_code.co_name?
    >
    > ###code begin###
    > #!/bin/python
    >
    > import sys
    >
    > def me():
    > t = sys._getframe(0)
    > return t.f_back.f_globals[t.f_back.f_code.co_name]
    > def fun1():
    > m = me
    > print me().data
    > def makefun () :
    > def tmpfunc():
    > print 'need something like me().data'
    > return tmpfunc
    >
    > fun1.s = fun1
    > fun1.data=['one', 'two', 'three']
    > fun1()
    > fun2 = makefun()
    > fun2.data=['four', 'five','six']
    > fun2()
    >
    > ###code end###
    >


    Not a direct answer to your question, but anyway;

    As soon as you want to bundle data with behaviour, OO comes to mind.
    Good news is that Python is actually an OOPL which implements functions
    as objects and let you define function-like ('callable') objects.

    class DataFunc(object):
    def __init__(self, data):
    self.data = data

    def __call__(self, *args, **kw):
    print self.data

    fun2 = DataFunc(['four', 'five', 'forty-two'])
    fun2()

    Note that you'll also have to correctly implement the __get__ method if
    you want an instance of DataFunc to be usable as a method (look for the
    descriptor protocol in the FineManual(tm) for more information on this
    point).

    HTH
    Bruno Desthuilliers, Jul 13, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. user
    Replies:
    1
    Views:
    420
    Kevin Spencer
    Jul 19, 2007
  2. Ulrich Dorda
    Replies:
    4
    Views:
    428
  3. thunk
    Replies:
    1
    Views:
    292
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    453
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    599
    thunk
    Apr 3, 2010
Loading...

Share This Page