How to make up lost reference to declare a field (numpy)?

强颜欢笑 提交于 2019-12-02 07:57:26

The only way I can think of is to march through the variables to figure out which variable it is, then assign it using self. Something like this:

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

    def init_x(self, x):
    # initialize or erase x to zeros
        names=self.__dict__.keys()
        varname=[name for name in names if getattr(self,name)==x]
        setattr(self,varname[0],np.zeros(self.n))

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.])

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.

The attribute can be modified directly:

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

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

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]]

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 is the value of the attribute, and does not, by itself, reference the attribute. f.x in this regard is just like other variables.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!