我是Python新手。我想在python中使用類似MATLAB的結構。我已經在使用字典的計算器中看到了幾種解決方案,例如this和this。不過,我覺得下面的代碼中的虛擬對象方法更像MATLAB,更自然,因爲我們不需要雙引號來表示屬性名稱。對我來說,這個用例是單親的,可以用一種隨便的方式將幾個對象包裝在一起,這樣一些函數就不需要長長的參數列表。例如,下面的最後一個函數只有兩個參數而不是6.MATLAB中的Python結構
我的問題是:現在我必須在腳本中定義虛擬類Structure。有必要嗎?如果我的許多腳本需要使用它,我將不得不把它放在我的所有函數都可以訪問的地方。是否有內置的「虛擬類」已經存在,我可以使用?
import numpy as np
class Structure:
pass
def construct_network():
net = Structure()
net.n = 100;
net.Weights = np.random.rand(net.n, net.n)
net.biases = np.random.rand(net.n, 1)
return net
def a_function_operate_on_two_nets(net1, net2):
# Use the net1 and net2 here.
查找到'namedtuple' – inspectorG4dget 2014-09-21 22:22:41