2012-05-24 191 views
0

我想做類似於this post的東西,但是在python中。Python - 將參數從一個函數傳遞給嵌套函數

基本上...我想從功能1(ABC)的自變量傳遞到函數2爲類型=(ABC)

僞代碼如下:基於僞代碼

function1 (*args, abc): 
    print xyz 

    function2(type=abc) 
+4

有什麼問題比爭論在'功能1()'秩序的其他的僞代碼? –

+0

python是一種動態語言,所以你不需要傳遞一個對象的類型。只是使用func1(* args)很好。如果你想處理這個類型,通過代碼檢查func2裏面:(type(args [1])== abc) – fanlix

回答

6

def function2(type): 
    print type 

def function1(abc, *args): 
    print "something" 
    function2(type=abc) 

>>> function1("blah", 1, 2, 3) 
something 
blah 

但基於你的鏈接問題,也許你想通過可變參數:

def function2(type, *args): 
    print type, args 

def function1(abc, *args): 
    print "something" 
    function2(abc, *args) 

>>> function1("blah", 1, 2, 3) 
something 
blah (1, 2, 3) 
-1

Python是動態類型的。雖然,類型轉換是一種選擇。

def foo(bar) 
    foo_bar(str(bar)) 

http://goo.gl/ixAY3

相關問題