2017-02-10 56 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!') 
相关问题