472,956 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,956 software developers and data experts.

access to the namespace of a function from within its invocation

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
Jul 13 '07 #1
1 1157
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
Jul 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark Miller | last post by:
I know this sounds really basic, but I'm having trouble with this within the VS.Net 2003 Pro IDE. I'm working with a VB.Net class library, and I'm having it reference a compiled assembly (a...
6
by: johny smith | last post by:
I was defining one of my own math functions for sin. So I thought I would create a unique name space. Then use that namespace in my program to call my custom sin math function. That way I was...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
6
by: SteveS | last post by:
Hello All. I have an asp.net application with 3 different assemblies. They are like this: 1) Assembly: PublicSite (This contains the website UI) Root namespace: PublicSite 2) Assembly:...
6
by: Larry Menard | last post by:
Folks, I know that DB2 does not (yet?) support this, but I wonder if anyone can suggest a work-around. I've seen article...
4
by: Kevin Newman | last post by:
The primary problem I've had with php is the lack of namespaces, which makes OOP very difficult to organize, since you end up with large number of classes cluttering up the same namespace - which...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
9
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
9
by: Mike | last post by:
Hi, Just a simple question: why the compiler doesn't report error when accessing a private member function inside a function having template type ? For example: #include<iostream> using...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.