2017-02-10 60 views
-1

我在Python中調用了一個像這樣的函數。位置參數跟隨關鍵字參數

order_id = kite.order_place(self, exchange, tradingsymbol, 
transaction_type, quantity, price, product, order_type, validity, 
disclosed_quantity=None, trigger_price=None, squareoff_value, 
stoploss_value, trailing_stoploss, variety, tag='') 

這裏是從功能的文檔代碼..

def order_place(self, exchange, tradingsymbol, transaction_type, 
quantity, price=None, product=None, order_type=None, validity=None, 
disclosed_quantity=None, trigger_price=None, squareoff_value=None, 
stoploss_value=None, trailing_stoploss=None, variety='regular', tag='') 

這是給這樣的錯誤..

enter image description here

如何解決這個問題? 謝謝!

+1

要麼添加更多的關鍵字,要麼刪除它們。 –

+0

錯誤消息告訴你*確切*出了什麼問題。看一些文檔。找出短語「位置參數」和「關鍵字參數」的含義。它不會殺了你。我承諾。 – 2017-02-10 16:15:58

+0

我已經找遍了。如果您知道解決方案,請發佈。我無法弄清楚什麼是錯的 –

回答

1

grammar of the language指定位置參數中的呼叫的關鍵字或出演參數之前出現:

argument_list  ::= positional_arguments ["," starred_and_keywords] 
          ["," keywords_arguments] 
          | starred_and_keywords ["," keywords_arguments] 
          | keywords_arguments 

具體地,關鍵字參數看起來像這樣:tag='insider trading!' 而一個位置參數看起來像這樣:..., exchange, ...。問題在於你似乎複製/粘貼了參數列表,並留下了一些默認值,這使得它們看起來像關鍵字參數而不是位置參數。這很好,除了你回到使用位置參數,這是一個語法錯誤。

此外,當參數有默認值,如price=None,這意味着你不必提供它。如果您不提供它,它將使用默認值。

要解決此錯誤,將您以後的位置參數到關鍵字參數,或者,如果他們有默認值,你不需要使用它們,根本就沒有指定他們:

order_id = kite.order_place(self, exchange, tradingsymbol, 
    transaction_type, quantity) 

# Fully positional: 
order_id = kite.order_place(self, exchange, tradingsymbol, transaction_type, quantity, price, product, order_type, validity, disclosed_quantity, trigger_price, squareoff_value, stoploss_value, trailing_stoploss, variety, tag) 

# Some positional, some keyword (all keywords at end): 

order_id = kite.order_place(self, exchange, tradingsymbol, 
    transaction_type, quantity, tag='insider trading!') 
相關問題