A Frame-space syntax ? - Re: global, globals(), _global ?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Mar 20 16:33:52 EST 2006


In <dvkifa$8mt$1 at ulysses.news.tiscali.de>, robert wrote:

> Marc 'BlackJack' Rintsch wrote:
>> In <dve6ll$29l4$1 at ulysses.news.tiscali.de>, robert wrote:
>> 
>>>The fact is:
>>>* Python has that big problem with unnecessary barriers for nested frame 
>>>access - especially painfull with callback functions where you want to 
>>>put back data into the calling frame.
>> 
>> 
>> You mean accessing the locals in the function that invoked the callback!? 
>> That sounds ugly.  Makes it hard to decouple the caller and the callee
>> here.
>> 
> 
> That is a frequent need. For read's its anyway wired up. E.g. callbacks:
> 
> def f():
>      a=1
>      def g(var):
>          print a             # automatic
>          .lastvar=var        # binds in outer frame
>      run_w_callback(1,2,g)
>      print lastvar
> 
> 
> Ruby blocks for example do that regularly.

I think you're trying to write Ruby programs in Python here.  Just define
a callable object as callback::

 class G:
     def __init__(self, a):
         self.a = a
         self.lastvar = None
    
     def __call__(self, var):
         print self.a
         self.lastvar = var

 def f2():
     g = G(1)
     run_w_callback(1, 2, g)
     print g.lastvar

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list