Numpy Matrix类:继承类的默认构造函数属性 [英] Numpy Matrix class: Default constructor attributes for inherited class

查看:281
本文介绍了Numpy Matrix类:继承类的默认构造函数属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现从numpy的矩阵类继承的矩阵类。



numpy的矩阵构造函数需要一个属性,例如 1 2; 3 4')。相反,我的构造函数应该不需要属性,并且应该为超级构造函数设置默认属性。



这是我做的:

  import numpy as np 

class MyMatrix(np.matrix):
def __init __ ):
super(MyMatrix,self).__ init __(1 2; 3 4)

如果__name__ ==__main__:
matrix = MyMatrix $ b

这段代码中必须有一个愚蠢的错误,因为我一直得到这个错误:

  this_matrix = np.matrix()
TypeError:__new __()至少需要2个参数b $ b

我真的很无能为力,而且Google目前还没有帮助。



谢谢!

解决方案

好问题!



从源代码看,似乎 np.matrix 数据 href =http://docs.python.org/2/reference/datamodel.html#object.__new__ =nofollow> __ new __ ,不在 __ init __ 中。这是违反直觉的行为,虽然我确定有一个很好的理由。



无论如何,下面的工作对我来说:

  class MyMatrix(np.matrix):
def __new __(cls):
注意,我们必须发送cls到super的__new__,我们给它超级已经。
#我认为这是因为__new__在技术上是一个静态方法,即使它应该是一个类方法
return super(MyMatrix,cls).__ new __(cls,1 2; 3 4)

mat = MyMatrix()

print mat
#outputs [[1 2] [3 4]]






附录:您可能想要考虑对所需的行为使用工厂函数,而不是子类。这将给你以下代码,它更短,更清晰,不依赖于 __ new __ -vs - __ init __ implementation detail:

  def mymatrix():
return np.matrix('1 2; 3 4 ')

mat = mymatrix()
print mat
#outputs [[1 2] [3 4]]
pre>

当然,您可能因为其他原因需要一个子类。


I want to implement my own matrix-class that inherits from numpy's matrix class.

numpy's matrix constructor requires an attribute, something like ("1 2; 3 4'"). In contrast, my constructor should require no attributes and should set a default attribute to the super-constructor.

That's what I did:

import numpy as np

class MyMatrix(np.matrix):
    def __init__(self):
        super(MyMatrix, self).__init__("1 2; 3 4")

if __name__ == "__main__":
    matrix = MyMatrix()

There must be a stupid mistake in this code since I keep getting this error:

this_matrix = np.matrix()
TypeError: __new__() takes at least 2 arguments (1 given)

I'm really clueless about that and googling didn't help so far.

Thanks!

解决方案

Good question!

From looking at the source, it seems as though np.matrix sets the data argument in __new__, not in __init__. This is counterintuitive behaviour, though I'm sure there's a good reason for it.

Anyway, the following works for me:

class MyMatrix(np.matrix):
    def __new__(cls):
        # note that we have to send cls to super's __new__, even though we gave it to super already.
        # I think this is because __new__ is technically a staticmethod even though it should be a classmethod
        return super(MyMatrix, cls).__new__(cls, "1 2; 3 4")

mat = MyMatrix()

print mat
# outputs [[1 2] [3 4]]


Addendum: you might want to consider using a factory function, rather than a subclass, for the behaviour you want. This would give you the following code, which is much shorter and clearer, and doesn't depend on the __new__-vs-__init__ implementation detail:

def mymatrix():
    return np.matrix('1 2; 3 4')

mat = mymatrix()
print mat
# outputs [[1 2] [3 4]]

Of course, you might need a subclass for other reasons.

这篇关于Numpy Matrix类:继承类的默认构造函数属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆