简体   繁体   English

如何在该类方法中创建类实例?

[英]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 AttributeError:矩阵实例没有__call__方法

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 NameError:未定义全局名称“matrix”

Of course matrix is not global class. 当然矩阵不是全局类。 I have no idea how to solve this problem. 我不知道如何解决这个问题。

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

要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM