How to instantiate a class inside this class method?

I want to create an instance of the class within myself. I tried to do it as follows:

class matrix: (...) def det(self): (...) m = self(sz-1, sz-1) (...) (...) 

but I got an error:

 m = self(sz-1, sz-1) 

AttributeError: the matrix instance has no __call__ method

So, I tried to do it as follows:

 class matrix: (...) def det(self): (...) m = matrix(sz-1, sz-1) (...) (...) 

and I got another error:

 m = matrix(sz-1, sz-1) 

NameError: global name 'matrix' not defined

Of course, a matrix is ​​not a global class. I do not know how to solve this problem.

+6
python class instance
source share
1 answer
 m = self.__class__(sz-1, sz-1) 

or

 m = type(self)(sz-1, sz-1) 
+11
source share

All Articles