Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > access to the namespace of a function from within its invocation

Reply
Thread Tools

access to the namespace of a function from within its invocation

 
 
Poor Yorick
Guest
Posts: n/a
 
      07-13-2007
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


 
Reply With Quote
 
 
 
 
John Nagle
Guest
Posts: n/a
 
      07-13-2007
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
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-13-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
access interactive namespace from module (shared namespace?) Ulrich Dorda Python 4 05-25-2008 12:56 PM
Custom Controls: Import a custom namespace and use its functions within user ASP .Net 1 07-19-2007 11:03 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57