2013-08-26 47 views
0

新手在這裏,所以請溫柔。我正在使用網格,並且我有一組包含原始輸入網格屬性的不可變列表(元組)。這些列表以及原始網格被用作組織指南,以便監視新創建的子網格;即由於子網引用原始網格,所以它的屬性(列表)可以用於在子網之間進行引用,或者創建子網的子網。婁列出的一些列表的結構:在Python中傳遞默認參數給函數

vertices = [ (coordinate of vertex 1), ... ] 
faceNormals = [ (components of normal of face 1), ...] 
faceCenters = [ (coordinates of barycenter of face 1), ...] 

因爲我不跟接力認識,我安排我的腳本如下所示:

def main(): 
    meshAttributes = GetMeshAttributes() 
    KMeans(meshAttributes, number_submeshes, number_cycles) 

def GetMeshAttributes() 
def Kmeans(): 
    func1 
    func2 
    ... 
def func1() 
def func2() 
... 
main() 

的問題是,在裏面KMEANS每個功能,我必須傳遞一些網格的屬性作爲參數,並且我不能默認它們,因爲它們在腳本開始時是未知的。例如內K均值是一個函數調用CreateSeeds:

def CreateSeeds(mesh, number_submeshes, faceCount, vertices, faceVertexIndexes): 

最後三個參數是靜態的,但我不能這樣做:

CreateSeeds(mesh, number_submeshes) 

,因爲我將不得不把faceCount, vertices, faceVertexIndexes裏面的函數定義,而這些清單在開始時是巨大的和未知的。

我嘗試過使用類,但是在我對它們的有限知識中,我遇到了同樣的問題。有人能給我一些關於在哪裏查找解決方案的提示嗎?

謝謝!

+2

有你試圖在互聯網上搜索您用作標題的確切短語? – filmor

回答

2

你想要的是獲得partial功能應用:如果你想指定最後參數您可以使用關鍵字參數

>>> from functools import partial 
>>> def my_function(a, b, c, d, e, f): 
...  print(a, b, c, d, e, f) 
... 
>>> new_func = partial(my_function, 1, 2, 3) 
>>> new_func('d', 'e', 'f') 
1 2 3 d e f 

lambda

>>> new_func = partial(my_function, d=1, e=2, f=3) 
>>> new_func('a', 'b', 'c') 
a b c 1 2 3 
>>> new_func = lambda a,b,c: my_function(a, b, c, 1, 2, 3) 
>>> new_func('a', 'b', 'c') 
a b c 1 2 3 
+0

太棒了!謝謝,我正在浪費時間尋找課程。 – grasshopper