Can a class instance also be a class?

Blair Hall b.hall at irl.cri.nz
Wed Aug 14 23:19:47 EDT 2002


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.

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.





More information about the Python-list mailing list