Getting Instance of calling class

Steven Taschuk staschuk at telusplanet.net
Tue Jun 17 12:24:32 EDT 2003


Quoth Thomas Güttler:
> I have a function called _() which prints 
> strings according to the language of the user.
> 
> I don't want to give this method the object which
> holds the language information every time I call it.
> 
> How can I access the calling object?

I'm not sure what you mean by "the calling object".  sys._getframe
provides frames from further up the call stack, but I'm not sure
how this relates to what you want to do.

It sounds a little like you want a dynamically scoped environment
containing (possibly among other things) localization information
for the current user.  I suppose you could implement such a thing
by trolling through stack frames, but this seems a bit hackish.

An alternative approach would be to pass _ into each function
which needs to produce output.  _ could be a closure,
    def makelocalizer(lang):
        def _(s):
            # return s in language lang
        return _
for example.  This avoids passing the language to _, but adds
passing _ around.  Does this help at all?

-- 
Steven Taschuk             "The world will end if you get this wrong."
staschuk at telusplanet.net     -- "Typesetting Mathematics -- User's Guide",
                                 Brian Kernighan and Lorrinda Cherry





More information about the Python-list mailing list