Getting Instance of calling class

Aahz aahz at pythoncraft.com
Tue Jun 17 20:35:24 EDT 2003


In article <mailman.1055866685.25954.python-list at python.org>,
Steven Taschuk  <staschuk at telusplanet.net> wrote:
>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?

Close.  _() should be an attribute of the user object.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it."  --Dijkstra




More information about the Python-list mailing list