Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create class instance inside that class method?

I want to create class instance inside itself. I tried to it by this way:

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

but I got error:

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

AttributeError: matrix instance has no __call__ method

So, I tried to do it by this way:

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' is not defined

Of course matrix is not global class. I have no idea how to solve this problem.

like image 802
pablo Avatar asked Jan 06 '10 18:01

pablo


1 Answers

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

or

m = type(self)(sz-1, sz-1)
like image 171
Daniel Roseman Avatar answered Nov 07 '22 20:11

Daniel Roseman