[TransWarp] Components + existing classes

Phillip J. Eby pje at telecommunity.com
Fri Jul 18 10:34:28 EDT 2003


At 07:35 PM 7/17/03 -0700, Joel Boehland wrote:

>I've been able to get an example to work, and I just wanted to run it by 
>the list to get comments on whether this is done the intended way. The 
>main problem I'm dealing with here, just to be clear, is: Correctly 
>writing Components that extend binding.Component AND some non-Component 
>class that has a non-empty __init__ method. If you don't have an __init__ 
>method, you can just use binding.New(<class>) to have it bound. For 
>non-empty init methods, you have to make a sort of factory method for that 
>component and use it in binding.Once(<factoryMeth>). At least that's the 
>way that I've done it. If that isn't the way to go, I'd appreciate some 
>pointers.

May I suggest simply overriding __init__ thusly:

def __init__(self, *__a, **__k):
     binding.Component.__init__(self, *__a, **__k)
     MyOtherBase.__init__(self, self.whatever, self.something...)

In other words, anything that's an init argument for MyOtherBase should be 
an attribute of self, allowing it to be controlled by bindings, either here 
or in subclasses.


>     #make a ChildFooCmp child of this component
>     def makeChild(self, instDict, attrName):
>         return ChildFooCmp(parentComponent=self, foo=self.sc_string)
>     #bind child to a component variable
>     child = binding.Once(makeChild, attrName="child")

May I also suggest naming the 'makeChild' function 'child', unless you want 
makeChild to be a public method of the class.




More information about the PEAK mailing list