spell method chaining?

gzeljko gzeljko at sezampro.yu
Sat Jun 9 03:47:22 EDT 2001


From: Robin Becker <robin at jessikat.fsnet.co.uk>
> 
> from __future__ import nested_scopes
> def gen(B):
>         class W(B):
>                 __base = B
>                 def __init__(self):
>                         self.__base.__init__(self)
                           ^^^^^^^^^^^^^^^^

>         return W
> 
> class C:
>         def __init__(self):
>                 pass
> 
> gen(gen(C))()
> 
> recurs infinitely.

Yes, 'cos You are searching __base always from self (instance object),
and found always the same __base. So You can't bind __base to class
object and achieve desired recursion, but ...

>  
> 
> from __future__ import nested_scopes
> def gen(B):
>         class W(B):
>                 def __init__(self):
>                         B.__init__(self)
>         return W
> 
> class C:
>         def __init__(self):
>                 pass
> 
> gen(gen(C))()

.... You can bind __base to metod what You effectively did here.

You can get it without nested scopes, with temporary variable:

import g # empty module, for globals

def gen(B):
    g.temp =  B
    class W(B):
        def __init__(self, base = g.temp):
            base.__init__(self)
    del g.temp        
    return W

I-didn't-proposed-that-ly gzeljko

> 
> -- 
> Robin Becker
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 






More information about the Python-list mailing list