Skip to content Skip to sidebar Skip to footer

How To Make Up Lost Reference To Declare A Field (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 Fo

Solution 1:

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

Solution 2:

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.


Post a Comment for "How To Make Up Lost Reference To Declare A Field (numpy)?"