Can a class instance also be a class?

Carl Banks imbosol at vt.edu
Thu Aug 15 06:07:10 CEST 2002


Blair Hall wrote:
> I'm betting that the answer is no, but if I am wrong please explain!
> 
> The background to my question is that I was wondering about the most
> appropriate ways of writing classes that are parameterized (like C++
> templates).
> 
> 
> My first choice was to wrap a class definition in a function, like
> 
> def _T_aClass(some_T):
>    class _aClass(object):
>        # ... uses some_T  for something in body
> 
>    return _aClass
> 
> This solution seems ok.

This is perfectly reasonable and concise; I suggest you not deviate
from it (at least until the optimization phase, at which point you
might want to cache the parameters).


> I wondered if a class could do something similar.
> My first attempt doesn't look good but perhaps there
> are improvements possible?
> 
> class _T_aClass(object):
> 
>    def __init__(self,some_T):
>        class _aClass(object):
>            # ... uses some_T  for something in body
>        self.__theClass
> 
>    def __call__(self):
>        return self.__theClass
> 
> If nothing else, I don't like the fact that an instance of _T_aClass is
> an object which when called
> like a function returns a class.

Why?  What does the above gain for you, besides more complexity in
both its defintion and use?


-- 
CARL BANKS
http://www.aerojockey.com



More information about the Python-list mailing list