![](/img/trans.png)
[英]How do I elegantly/efficiently write the __init__ function for a Python class that takes lots of instance variables?
[英]How do I pass lots of variables to and from a function in Python?
我做科學編程,並且經常想要向用戶顯示提示和變量對,讓他們編輯變量,然后用新變量進行計算。 我經常這樣做,我寫了一個wxPython類來將這段代碼移出主程序。 您可以使用變量類型(字符串,浮點數,整數),提示符和變量的當前值為每個變量設置一個列表。 然后將所有這些列表放在一個大的列表中,我的實用程序創建了一個整齊的wxPython面板,其中包含提示和可以編輯的當前值。
當我開始時,我只有一些變量,所以我會寫出每個變量。
s='this is a string'; i=1; f=3.14
my_list=[ ['s','your string here',s], ['i','your int here',i], ['f','your float here'],]
input_panel = Input(my_list)
# the rest of the window is created, the input_panel is added to the window, the user is
# allowed to make choices, and control returns when the user hits the calculate button
s,i,f = input_panel.results() # the .results() function returns the values in a list
現在我想將這個例程用於很多變量(10-30),這種方法正在破裂。 我可以使用list.append()語句通過多行創建函數的輸入列表。 但是,當代碼從函數返回時,我得到了這個龐大的列表,需要將其解壓縮到正確的變量中。 這很難管理,看起來很容易讓輸入列表和輸出列表不同步。 更糟糕的是,它看起來很糟糕。
使用額外信息將大量變量傳遞給Python中的函數的最佳方法是什么,以便可以對它們進行編輯,然后將變量返回以便我可以在程序的其余部分中使用它們?
如果我可以通過引用將變量傳遞給函數,那么用戶可以更改它們,並且一旦程序從函數返回,我將使用這些值。 我只需要在多行上構建輸入列表,輸入列表就不會與輸出列表不同步。 但Python不允許這樣做。
我是否應該將大型列表拆分為較小的列表,然后將這些列表組合成大型列表以進入和退出函數? 或者這只是添加更多地方來制造錯誤?
最簡單的方法是創建一個類。 該類將具有屬性,而不是處理變量列表。 然后你只使用該類的一個實例。
我想到了兩個不錯的選擇。
第一種是使用字典在一個地方收集所有變量:
d = {}
d['var1'] = [1,2,3]
d['var2'] = 'asdf'
foo(d)
第二種是使用類來捆綁所有參數。 這可能很簡單:
class Foo(object):
pass
f = Foo()
f.var1 = [1,2,3]
f.var2 = 'asdf'
foo(f)
在這種情況下,我更喜歡這個類而不是字典,只是因為你最終可以為類提供一個定義以使其更清晰,或者提供處理一些打包和解包工作的方法。
對我來說,理想的解決方案是使用這樣的類:
>>> class Vars(object):
... def __init__(self, **argd):
... self.__dict__.update(argd)
...
>>> x = Vars(x=1, y=2)
>>> x.x
1
>>> x.y
2
您還可以構建一個字典並將其傳遞給它:
>>> some_dict = {'x' : 1, 'y' : 2}
>>> #the two stars below mean to pass the dict as keyword arguments
>>> x = Vars(**some_dict)
>>> x.x
1
>>> x.y
2
然后,您可以在將數據傳遞給函數時根據需要獲取數據或更改數據:
>>> def foo(some_vars):
... some_vars.z = 3 #note that we're creating the member z
...
>>> foo(x)
>>> x.z
3
如果我可以通過引用將變量傳遞給函數,那么用戶可以更改它們,並且一旦程序從函數返回,我將使用這些值。
你可以通過傳遞一個dict
獲得與“通過引用傳遞”相同的效果(或者為了語法方便而獲得一個Bunch
,請參閱http://code.activestate.com/recipes/52308/ )。
如果您有一組有限的這些情況,您可以為每個情況編寫特定的包裝函數。 每個包裝器都會執行構建和解包列表的工作,這些列表將傳遞給內部函數。
這樣,您只需要一次又一次地修改代碼的一小部分可管理部分。 當然,如果您習慣使用類,則應將所有這些封裝到類中。
"""Store all variables
"""
vars = {}
"""Store the order of display
"""
order = []
"""Define a function that will store details and order of the variable definitions
"""
def makeVar(parent, order, name, value, prompt):
parent[name] = dict(zip(('value', 'prompt'), (value, prompt)))
order.append(name)
"""Create your variable definitions in order
"""
makeVar(vars, order, 's', 'this is a string', 'your string here')
makeVar(vars, order, 'i', 1, 'your int here')
makeVar(vars, order, 'f', 3.14, 'your float here')
"""Use a list comprehension to prepare your input
"""
my_list = [[name, vars[name]['prompt'], vars[name]['value']] for name in order]
input_panel = Input(my_list)
out_list = input_panel.results();
"""Collect your output
"""
for i in range(0, len(order)):
vars[order[i]]['value'] = out_list[i];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.