位置参数跟随关键字参数

时间:2017-02-10 16:12:52

标签: python

我在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 :(得分:13)

grammar of the language指定位置参数出现在调用中的关键字或星号参数之前:

rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile("/path/ca-cert.pem")
if err != nil {
   log.Fatal(err)
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
   log.Fatal("Failed to append PEM.")
}
mysql.RegisterTLSConfig("custom", &tls.Config{
                         RootCAs: rootCertPool,
                        })
db, err := sql.Open("mysql", "user@tcp(localhost:3306)/test?tls=custom")

具体来说,关键字参数如下所示:argument_list ::= positional_arguments ["," starred_and_keywords] ["," keywords_arguments] | starred_and_keywords ["," keywords_arguments] | keywords_arguments 而位置参数如下所示:tag='insider trading!'。问题在于您似乎已复制/粘贴参数列表,并保留了一些默认值,这使它们看起来像关键字参数而不是位置参数。这很好,除了你然后回到使用位置参数,这是一个语法错误。

此外,当参数具有默认值时,例如..., exchange, ...,这意味着您不必提供它。如果您不提供,则会使用默认值。

要解决此错误,请将以后的位置参数转换为关键字参数,或者,如果它们具有默认值且您不需要使用它们,则根本不要指定它们:

price=None
相关问题