How to get a Function object from a Frame object
Hi, I need access to a function object that corresponds to a frame object in a certain case from inside the function. I can get the frame object using: f = sys._getframe(0) But the resulting frame object doesn't contain the information I need. There is a lot of information in the code object (f.f_code), but not the actual function or method object. The inspect module doesn't help either it just provides nicer interface to the Frame object. Any ideas out there? Thanks, Gigi -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get a Function object from a Frame object
Greg Ewing wrote: Steven Bethard wrote: Gigi wrote: I need access to a function object that corresponds to a frame object in a certain case from inside the function. Can you look up the code's co_name in the previous frame's locals? You probably also need to check the previous frame's globals... And there are probably some cases where this code still wouldn't work... Note that in general it's impossible to tell exactly which function object was involved, since there could be more than one function object sharing the same code object, and the frame only references the code object. I can get the co_name and everything that's available from the code object. However, I can't get to the actual function object. I need the function object to get a custom function attribute that was injected earlier as context. I didn't know that code objects could be shared. I guess it really makes the whole thing impossible, unless the code object kept a list of all the functions that share it. Thanks, anyway. I found a different solution. -- http://mail.python.org/mailman/listinfo/python-list
__getattribute__ and __getattr__
Hi, In the Python documentation regarding __getattribute__ (more attribute access for new style classes) it is mentioned that if __getattribute__ is defined __getattr__ will never be called (unless called explicitely). Here is the exact citation: """ The following methods only apply to new-style classes. __getattribute__( self, name) Called unconditionally to implement attribute accesses for instances of the class. If the class also defines __getattr__, it will never be called (unless called explicitly). This method should return the (computed) attribute value or raise an AttributeError exception. In order to avoid infinite recursion in this method, its implementation should always call the base class method with the same name to access any attributes it needs, for example, "object.__getattribute__(self, name)". """ I discovered that it is not so for Python 2.3.4 on Windows at least. The actual behavior is that if both __getattribute__ and __getattr__ methods exist then __getattribute__ is called first, but if it raises AttributeError then the exception will be swallowed silently and __getattr__ will be invoked. Note that if I forward to the default object.__getattribute__ or if I raise the AttributeError myself the result is the same. My understanding of the documentation is it that the program should just exit with the AttributeError exception. Here is the code: class A(object): def __getattribute__(self, name): return object.__getattribute__(self, name) # raise AttributeError() def __getattr__(self, name): return 42 if __name__ == '__main__': a = A() print a.x Here is the Output: 42 -- http://mail.python.org/mailman/listinfo/python-list
Re: __getattribute__ and __getattr__
Terry Reedy wrote: > "Gigi" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>Hi, >>In the Python documentation regarding __getattribute__ (more attribute >>access for new style classes) it is mentioned that if __getattribute__ >>is defined __getattr__ will never be called (unless called explicitely). >>Here is the exact citation: > > > Discrepancy reported in > > https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1204734&group_id=5470 > > Guido declared behavior right and doc wrong and in need of correction. > > Terry J. Reedy > > > Thanks for your reply. It definitely clears the issue. I'm writing an article to Dr. Dobbs about the Python object model and I wouldn't want to be inaccurate. I fill a little like Leibnitz :-) . It's a curious coincidence that two separate people notice the same documentation issue that lingere for such a long time in such a short time span. Thanks, Gigi -- http://mail.python.org/mailman/listinfo/python-list
Re: __getattribute__ and __getattr__
Terry Reedy wrote: > "Gigi" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>Hi, >>In the Python documentation regarding __getattribute__ (more attribute >>access for new style classes) it is mentioned that if __getattribute__ >>is defined __getattr__ will never be called (unless called explicitely). >>Here is the exact citation: > > > Discrepancy reported in > > https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1204734&group_id=5470 > > Guido declared behavior right and doc wrong and in need of correction. > > Terry J. Reedy > > > Thanks for your reply. It definitely clears the issue. I'm writing an article to Dr. Dobbs about the Python object model and I wouldn't want to be inaccurate. I fill a little like Leibnitz :-) . It's a curious coincidence that two separate people notice the same documentation issue that lingere for such a long time in such a short time span. Thanks, Gigi -- http://mail.python.org/mailman/listinfo/python-list
Re: __getattribute__ and __getattr__
Terry Reedy wrote: > "Gigi" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > >>Hi, >>In the Python documentation regarding __getattribute__ (more attribute >>access for new style classes) it is mentioned that if __getattribute__ >>is defined __getattr__ will never be called (unless called explicitely). >>Here is the exact citation: > > > Discrepancy reported in > > https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1204734&group_id=5470 > > Guido declared behavior right and doc wrong and in need of correction. > > Terry J. Reedy > > > Thanks for your reply. It definitely clears the issue. I'm writing an article to Dr. Dobbs about the Python object model and I wouldn't want to be inaccurate. I fill a little like Leibnitz :-) . It's a curious coincidence that two separate people notice the same documentation issue that lingere for such a long time in such a short time span. Thanks, Gigi -- http://mail.python.org/mailman/listinfo/python-list