问题
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
but method
def init_x(self, x):
# initialize or erase x to zeros
x = np.zeros(self.n)
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
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)
since
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
回答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))
回答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.
来源:https://stackoverflow.com/questions/51562730/how-to-make-up-lost-reference-to-declare-a-field-numpy