Callable modules?

holger krekel pyth at devel.trillke.net
Thu Jul 25 06:57:37 EDT 2002


Fernando Perez wrote:
> holger krekel wrote:
> 
> > from somemodule.someclass import filter1
> 
> What about:
> 
> #somemodule.py
> def filter1(self,args):
>   ...
> 
> def filter2(self,args):
>   ...
> 
> class someclass:
>   ...
> 
> someclass.filter1 = filter1
> someclass.filter2 = filter2
> ...
> 
> # end somemodule.py
> 
> I know there's still one little layer of redundant typing, but it only has to 
> be done in the module which defines your class, not by the users. So you do 
> it once and you are done.

Yes. I'd even prefer automating this somewhat more (via a loop over
the filter function in which 'setattr(someclass,name,obj)' is performed.

But still i don't see any reason, why 

    from somemodule.someclass import somenames

should not work. 

> ps. I guess you lose safety checks on what 'self' is when you invoke them as 
> standalones, ...

but you gain flexibility :-)

    holger




More information about the Python-list mailing list