Getting Instance of calling class

anton muhin antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Tue Jun 17 12:04:04 CEST 2003


Thomas Güttler wrote:
> Hi!
> 
> 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?
> 
> Background: This is a python product for zope.
> My main object holds the prefered language of each
> user. 
> 
> I found no way to get a reference to my main
> object from _(). Global variables are no solution
> since there could be several main objects and there
> are multiple threads.
> 
>  thomas
> 
You can find a caller with get_frame trickery (google for it---this 
issue has been discussed already). However, I think you should stick to 
another solution. As a simple example:

class UserInfo:
    ...
    def print_msg(msg):
        # Language dependent code goes here

when later:
      user = current_user()
      report_function = user.print_msg
      callee(report_function, ....)

hth,
anton.





More information about the Python-list mailing list