我正在尝试传递属性。所以我有一个类,它有2个属性,例如freq,response: 最初我没有检查。如果nargin = 1,则给出给定参数(nargin): 我将参数切成freq和response,以便传递给带有2个参数的函数:
def Freq_domain_data_new(*args):
S = args[0]
obj = FreqDomainData()
[nargin, varargin] = foo(args)
if nargin == 1:
#obj = Freq_domain_data_new(args[0].response, args[0].freq)
print(args[0].response, "frequency", args[0].freq)
我可以看到输出为
(array([[[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j]],
[[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+1.78200370e-02j,
2.76164246e-02+3.69176314e-02j, ...,
-6.19449569e-03+5.94685993e-02j,
-6.49425365e-03+5.72912490e-02j,
-6.30085891e-03+5.52413652e-02j],
[ 0.00000000e+00+0.00000000e+00j,
-5.64409958e-03+0.00000000e+00j,
0.00000000e+00+2.92717217e-03j, ...,
-2.34409953e-04+1.11168204e-01j,
-2.96739422e-04+1.11054836e-01j,
-3.20859817e-04+1.10975109e-01j],
[ 0.00000000e+00+0.00000000e+00j,
-3.30150288e-04+1.10923258e-01j,
-3.46741026e-04+0.00000000e+00j, ...,
-2.73295572e-04+1.63302152e-02j,
-3.14846040e-04+1.62408281e-02j,
-3.24518860e-04+1.61656951e-02j],
[ 0.00000000e+00+0.00000000e+00j,
-3.09627229e-04+1.61104783e-02j,
-2.80500203e-04+1.60755258e-02j, ...,
4.46651587e-02+4.55428277e-02j,
3.55042185e-02+5.20892818e-02j,
2.46735854e-02+5.61128929e-02j],
[ 0.00000000e+00+0.00000000e+00j,
1.26956204e-02+5.74266512e-02j,
1.47258815e-04+5.59734534e-02j, ...,
8.24447781e-03-1.11059426e-03j,
8.71037166e-03+8.26010321e-04j,
8.75487889e-03+2.73296635e-03j]],
[[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
8.36908343e-03+4.51295452e-03j,
7.56682781e-03+6.07314245e-03j, ...,
1.01183427e-03-1.47423852e-03j,
1.37387441e-03-1.13807905e-03j,
1.67175010e-03-7.40504089e-04j],
[ 0.00000000e+00+0.00000000e+00j,
1.88782683e-03-3.01974725e-04j,
2.00879240e-03+1.53991921e-04j, ...,
9.21787274e-04+1.11678713e-01j,
6.13821618e-04+1.11672538e-01j,
3.21209186e-04+1.11589676e-01j],
[ 0.00000000e+00+0.00000000e+00j,
7.34031467e-05+1.11458144e-01j,
-1.12059361e-04+1.11308759e-01j, ...,
2.19704000e-03+5.18470996e-02j,
5.25239993e-04+5.10229865e-02j,
-9.95136992e-04+4.98672311e-02j],
[ 0.00000000e+00+0.00000000e+00j,
-2.27328386e-03+4.84528258e-02j,
-3.23575567e-03+4.68691200e-02j, ...,
7.71541300e-04+1.12261234e-01j,
2.86794931e-04+1.12170169e-01j,
-1.71608877e-04+1.11965474e-01j],
[ 0.00000000e+00+0.00000000e+00j,
-5.65065234e-04+1.11679634e-01j,
-8.67122987e-04+1.11350397e-01j, ...,
1.74310194e-03-7.76130089e-03j,
3.49617853e-03-6.82626142e-03j,
5.09010326e-03-5.56856901e-03j]],
[[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
6.45197776e-03-4.04128575e-03j,
7.51641536e-03-2.31147574e-03j, ...,
5.01873663e-02-1.42387213e-02j,
5.44862636e-02-1.99470112e-03j,
5.61427862e-02+1.02832427e-02j],
[ 0.00000000e+00+0.00000000e+00j,
5.50939360e-02+2.20088482e-02j,
5.14047455e-02+3.26254085e-02j, ...,
9.32099581e-04-8.12199847e-03j,
2.75056685e-03-7.33378635e-03j,
4.44118399e-03-6.19976017e-03j],
[ 0.00000000e+00+0.00000000e+00j,
5.92695928e-03-4.76837036e-03j,
7.13790768e-03-3.10259070e-03j, ...,
4.09342491e-04+1.59887238e-02j,
4.77753495e-04+1.61349188e-02j,
5.02121101e-04+1.62824306e-02j],
[ 0.00000000e+00+0.00000000e+00j,
4.78498435e-04+1.64143842e-02j,
4.09443374e-04+1.65162001e-02j, ...,
2.08096048e-03+1.10791634e-01j,
2.07696980e-03+1.11282985e-01j,
1.92309248e-03+1.11708876e-01j],
[ 0.00000000e+00+0.00000000e+00j,
1.63223032e-03+1.12029008e-01j,
1.23362037e-03+1.12216569e-01j, ...,
9.42084800e-03+6.38729058e-02j,
8.46559123e-03+6.48855335e-02j,
7.24047232e-03+6.55970583e-02j]],
[[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
5.80518610e-03+6.59603907e-02j,
4.23616465e-03+6.59495142e-02j, ...,
-1.94784898e-03-4.55469963e-04j,
-1.88311859e-03-8.42335960e-04j,
-1.73529279e-03-1.18814856e-03j],
[ 0.00000000e+00+0.00000000e+00j,
-1.51245132e-03-1.48008701e-03j,
-1.22442576e-03-1.70706382e-03j, ...,
-7.19711308e-03-6.06231948e-03j,
-5.99912323e-03-7.19443251e-03j,
-4.52990960e-03-7.98235958e-03j],
[ 0.00000000e+00+0.00000000e+00j,
-2.85978045e-03-8.39430511e-03j,
-1.06582113e-03-8.41443101e-03j, ...,
-2.34361992e-03-5.76961699e-02j,
1.05610226e-02-5.38494077e-02j,
2.28875606e-02-4.74130570e-02j],
[ 0.00000000e+00+0.00000000e+00j,
3.40410110e-02-3.87147475e-02j,
4.34882259e-02-2.81890444e-02j, ...,
-5.17261015e-02-4.08138444e-02j,
-4.37448296e-02-4.92756912e-02j,
-3.36856051e-02-5.53149787e-02j],
[ 0.00000000e+00+0.00000000e+00j,
-2.20471788e-02-5.86519762e-02j,
-9.40292415e-03-5.91410743e-02j, ...,
-8.10480312e-03+1.62808701e-03j,
-8.63373450e-03-2.96232357e-04j,
-8.73937886e-03-2.18292093e-03j]],
[[ 0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j, ...,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j,
0.00000000e+00+0.00000000e+00j],
[ 0.00000000e+00+0.00000000e+00j,
-8.42501600e-03-3.94312414e-03j,
-7.71410383e-03-5.49716686e-03j, ...,
-9.60091428e-04+1.50877426e-03j,
-1.31731727e-03+1.17571237e-03j,
-1.60392779e-03+7.92888417e-04j],
[ 0.00000000e+00+0.00000000e+00j,
-1.80818690e-03+3.80341417e-04j,
-1.92333621e-03-4.18821516e-05j, ...,
3.09671196e-03+4.86273910e-02j,
5.09211914e-03+4.92400980e-02j,
6.98676423e-03+5.02204621e-02j],
[ 0.00000000e+00+0.00000000e+00j,
8.71031176e-03+5.15298321e-02j,
1.01980692e-02+5.31202436e-02j, ...,
-8.56182044e-04+1.10617053e-01j,
-8.98283441e-04+1.10360102e-01j,
-8.91155582e-04+1.10074233e-01j],
[ 0.00000000e+00+0.00000000e+00j,
-8.08143975e-04+1.09788987e-01j,
-6.30922012e-04+1.09541826e-01j, ...,
-1.48278058e-04+1.60834067e-02j,
-1.49333723e-04+1.60654969e-02j,
-1.57614059e-04+1.60297548e-02j],
[ 0.00000000e+00+0.00000000e+00j,
-1.62651621e-04+1.59786034e-02j,
-1.53429403e-04+1.59197798e-02j, ...,
1.91204240e-04+9.01142745e-03j,
-1.84876912e-03+8.35345358e-03j,
-3.78213323e-03+7.28649528e-03j]]]), 'frequency', array([0.00000000e+000, 1.00000000e+007, 2.00000000e+007, ...,
2.26349788e-315, 2.26349828e-315, 2.26349867e-315]))
现在,当我运行此
def Freq_domain_data_new(*args):
S = args[0]
obj = FreqDomainData() #I have a created a class
[nargin, varargin] = foo(args)
if nargin == 1:
obj = Freq_domain_data_new(args[0].response, args[0].freq)
print(args[0].response, "frequency", args[0].freq)
我收到错误提示:
File "C:\Users\nkorgaon\PycharmProjects\HelloWorld\try_nikita.py", line 102, in Freq_domain_data_new
obj = Freq_domain_data_new(args[0].response, args[0].freq)
**AttributeError: 'numpy.ndarray' object has no attribute 'response'**
我是python n Stackoverflow的新手:)如果有人可以提供帮助,那就太好了。事先为错别字道歉:)
答案 0 :(得分:0)
def Freq_domain_data_new(*args):
S = args[0]
obj = FreqDomainData() #I have a created a class
[nargin, varargin] = foo(args)
if nargin == 1:
obj = Freq_domain_data_new(args[0].response, args[0].freq)
print(args[0].response, "frequency", args[0].freq)
也许:
最初,您使用参数作为参数调用Freq_domain_data_new
,这些参数的第一项是具有response
和freq
属性的对象-其response
属性是Numpy ndarray。然后...
obj = Freq_domain_data_new(args[0].response, args[0].freq)
在函数中,您进行递归
调用传递一个ndarray(args[0].response
)和其他内容(args[0].freq
)。
当 new 函数执行同一行时,它会产生异常,因为它的args[0]
是一个ndarray。
这种事情正在发生:
class A:
def __init__(self):
self.response = np.arange(5)
self.freq = np.arange(5,9)
def __repr__(self):
return 'A instance'
def foo(*args):
print(f'args: {args}')
print('calling foo again')
obj = foo(args[0].response, args[0].freq) # <--- recursive call
a = A()
foo(a, 'args[1]')
这将导致
>>>
args: (A instance, 'args[1]')
calling foo again
args: (array([0, 1, 2, 3, 4]), array([5, 6, 7, 8]))
calling foo again
Traceback (most recent call last):
File "C:\pyProjects33\tmp.py", line 38, in <module>
foo(a, 'args[1]')
File "C:\pyProjects33\tmp.py", line 34, in foo
obj = foo(args[0].response, args[0].freq)
File "C:\pyProjects33\tmp.py", line 34, in foo
obj = foo(args[0].response, args[0].freq)
AttributeError: 'numpy.ndarray' object has no attribute 'response'
>>>