如何弥补丢失的引用来声明一个字段(numpy)? [英] How to make up lost reference to declare a field (numpy)?

查看:65
本文介绍了如何弥补丢失的引用来声明一个字段(numpy)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个包含很多字段的类,并且我想创建一个对所有字段都适用的初始化程序,这样我就不必为每个字段编写一个初始化程序.

Let's say I have class that contains a lot of fields and I want to make initializer that work for all fields so that I don't need to write an initializer for each of them.

class Foo():
    def __init__(n):
        self.n = n
        self.x = None
        self.m = None
        self.v = None

但是方法

def init_x(self, x):
    # initialize or erase x to zeros
    x = np.zeros(self.n)

不起作用,因为x=失去了对x的引用.但是

doesn't work because x= loses reference to x. But

@staticmethod
def get_x(x, i):
    return x[i]

@staticmethod
def set_x(x, i):
    x[i] = val

有效.什么可能使我的init_x正常工作?最初我想将这些字段设置为无",因为还有其他一些事情取决于这些字段是否为无.

works. What could possibly make my init_x to work? I want to set the fields to None initially cuz there are other things that I depend on whether these fields are None or not.

重新发表评论之一,以下内容无效

Re one of the comment, below doesn't work

from numpy import zeros

class Foo:
  def __init__(self):
    self.x = None

  @staticmethod
  def foo(x):
    x[:] = zeros((10,4))

  def make(self):
    self.foo(self.x)


f = Foo()
f.make()
print(f.x)

因为

Traceback (most recent call last):
  File "python", line 16, in <module>
  File "python", line 12, in make
  File "python", line 9, in foo
TypeError: 'NoneType' object does not support item assignment

推荐答案

一个属性的初始化程序应写为:

An initializer for one attribute would be written as:

class Foo():
    def __init__(self, n):
        self.n = n
        self.x = None
    def set_x(self):
        self.x = np.zeros(self.n)
    def __repr__(self):
        return 'Foo {} {}'.format(self.n, self.x)

In [67]: f = Foo(3)

In [68]: f
Out[68]: Foo 3 None

In [69]: f.set_x()

In [70]: f
Out[70]: Foo 3 [0. 0. 0.]

In [71]: f.x
Out[71]: array([0., 0., 0.])

也许这说明了显而易见的内容,但是我必须仔细阅读注释,以更清楚地了解您想推广set_x以便以某种方式使用一组属性.

Maybe this is stating the obvious, but I have to dig through the comments to get a clearer sense that you want to generalize set_x to somehow work with a set of attributes.

可以直接修改属性:

In [72]: f.x = np.zeros((1,f.n),int)

In [73]: f
Out[73]: Foo 3 [[0 0 0]]

一旦f.x是一个数组,我们就可以对其进行突变.但是,如果它是None,则不会起作用:

Once f.x is an array we can mutate it. But this won't work if it is None:

In [74]: f.x[0,:]=[1,2,3]

In [75]: f
Out[75]: Foo 3 [[1 2 3]]

我们可以通过__dict__通过字符串名称访问属性:

We could access the attribute by string name, via the __dict__:

In [79]: f.__dict__['x']=None

In [80]: f
Out[80]: Foo 3 None

In [81]: f.__dict__['x']=np.arange(f.n*2)

In [82]: f
Out[82]: Foo 3 [0 1 2 3 4 5]

将其包装在方法中应该很容易.

It should be easy to wrap this in a method.

f.x是属性的值,并且本身不引用该属性.在这方面f.x就像其他变量一样.

f.x is the value of the attribute, and does not, by itself, reference the attribute. f.x in this regard is just like other variables.

这篇关于如何弥补丢失的引用来声明一个字段(numpy)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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