Personal tools
You are here: Home Zope & Plone tips Get the class that initiated a call from a function that is called
Navigation
 
Document Actions
  • Send this page to somebody
  • Print this page
  • Add Bookmarklet

Get the class that initiated a call from a function that is called

by tibi last modified 2008-10-25 18:51

Somewhere in the sources of Zope I've seen a technique similar to this, to get to the locals of the caller object. Although interesting to know, actually deploying this technique makes the code hard to understand, so I would advice against it. Still, I'm recording it here, for my own reference.

import sys

def do():
    frame = sys._getframe(1)
    caller = frame.f_locals['self']
    print caller.secret

class A(object):
    secret = "Hello world"
    def __init__(self):
        do()

if __name__ == "__main__":
    A()

 

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: